参考链接: https://blog.csdn.net/qq_46664185/article/details/122437700
# 1.安装node.js
Node.js 中文网
# 2.全局安装electron
npm install electron-packager -g
# 3.创建目录
找到你前端页面的文件夹,package.json、main.js、index.html三个文件(当然也可以npm init创建),这个index.html就是你的网页首页。
项目结构
├── package.json
├── main.js
└── index.html
这个是意思是先创建一个文件夹,然后要打包的文件夹里必须要有package.json、main.js、index.html三个文件这三个文件,cocos打的好的文件夹里只有index.html,所以package.json、main.js这两个自己新建,main.js的内容
```
const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win
function createWindow () {
// Create the browser window.
win = new BrowserWindow({width: 1280, height: 720})
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
win.setMenu(null);//关掉菜单
// Open the DevTools.
// win.webContents.openDevTools()
// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
//下面我的
// electron-packager . app --win --out presenterTool --arch=x64 --overwrite --ignore=node_modules
// electron-packager . helloworld --platform=win32 --arch=x64 --icon=icon.ico --out=./out --asar --app-version=1.1.0 --overwrite --ignore=node_modules --electron-version 19.0.2
```
然后package.json里的内容:
```
{
"name" : "中文名",
"version" : "2.6.0",
"main" : "main.js",
"scripts": {
"start": "electron . ",
"dev": "electron . --env=local",
"test": "set DEBUG=* && electron . --env=local",
"build-w": "electron-builder -w=nsis --ia32",
"build-w-64": "electron-builder -w=nsis --x64",
"build-w-arm64": "electron-builder -w=nsis --arm64",
"build-wz": "electron-builder -w=7z --ia32",
"build-wz-64": "electron-builder -w=7z --x64",
"build-wz-arm64": "electron-builder -w=7z --arm64",
"build-m": "electron-builder -m",
"build-m-arm64": "electron-builder -m --arm64",
"build-l": "electron-builder -l=deb --ia32",
"build-l-64": "electron-builder -l=deb --x64",
"build-l-arm64": "electron-builder -l=deb --arm64",
"build-l-armv7l": "electron-builder -l=deb --armv7l",
"build-lr-64": "electron-builder -l=rpm --x64",
"build-lp-64": "electron-builder -l=pacman --x64",
"rd": "ee-core rd --dist_dir=./frontend/dist",
"encrypt": "ee-core encrypt",
"rebuild": "electron-rebuild",
"package":"electron-packager . 美视达 --platform=win32 --arch=x64 --icon=icon.ico --out=./out --asar --app-version=1.0.0 --overwrite --ignore=node_modules"
}
}
```
其中"start": "electron . ",必有,
然后:
"package":
"electron-packager . 美达 --platform=win32 --arch=x64 --icon=icon.ico --out=./out --asar --app-version=1.0.0 --overwrite --ignore=node_modules" 这两个很重要,
```
参数说明:
美达 :你将要生成的exe文件的名称
–platform=win32:确定了你要构建哪个平台的应用(Windows、Mac 还是 Linux),可取的值有 darwin, linux, mas, win32
–arch=x64:决定了使用 x86 (ia32)还是 x64(x64),还是两个架构都用
–icon=自定义.ico:自定义设置应用图标
–out=./out:指定打包文件输出的文件夹位置,当前指定的为项目目录下的release文件夹
–asar:该参数可以不加,如果加上,打包之后应用的源码会以.asar格式存在
–app-version=1.0.0:生成应用的版本号
–overwrite:覆盖原有的build,让新生成的包覆盖原来的包
–ignore=node_modules:如果加上该参数,项目里node_modules模块不会被打包进去
–electron-version 8.2.1:指定当前要构建的electron的电子版本(不带"v"),需要和当前的版本一致,具体可以在 package.json文件中查看,可以不加该参数,如果不一致,会自动下载。
```
按shift+鼠标右键,选择进入powershell 窗口,然后输入npm run package ,就可以了,一般就可以打包成功了,
注意:这样出来的不是安装包,只是一个文件夹,里面有一个exe,可以运行,但无法安装,
然后借助visual studio,打包的