經過npm全局安裝electronhtml
npm install electron -g
這樣你就能夠在電腦的任意位置進行你想要的操做了node
package.json內的文件內容web
{ "name": "app-name", "version": "0.1.0", "main": "main.js" }
main.js內的內容npm
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: 800, height: 600 }) // and load the index.html of the app. win.loadURL( url.format({ pathname: path.join(__dirname, 'index.html'), protocol: 'file:', slashes: true, }), ) // 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-packagjson
npm install electron-package -g
最後一步進行打包操做,這塊比較代碼比較長
electron-package . (生成exe文件的名字) --win --out (打包完文件夾的名字) -arch=×64 --electronVersion (electron的版本號) --overwrite --ignore=node_modules便可完成打包
例如:個人項目須要進行打包的操做爲:windows
electron-packager . miaotong --win --out presenterTool --arch=x64 --electronVersion 3.0.4 --overwrite --ignore=node_modules
注意:--electronVersion的版本號必須和你第一步安裝的electron版本一致,若是不肯定版本能夠輸入命令進行查看
cmd----->electron -v 終端會輸出你當前全局安裝electron的版本號,固然這個操做也是你驗證electron有沒有安裝成功的方法瀏覽器
項目展現:app