Starting from the directory where you’ve copied your application, take the following steps to convert your app to an Electron-based desktop application.
npm install electron electron-builder wait-on concurrently --only=dev
npm install electron-is-dev
public
folder of your app, public/start-electron.js
, with the following code:
const electron = require('electron'),
app = electron.app,
BrowserWindow = electron.BrowserWindow;
const path = require('path'),
isDev = require('electron-is-dev');
let mainWindow;
const createWindow = () => {
mainWindow = new BrowserWindow({ width: 480, height: 320 })
const appUrl = isDev ? 'http://localhost:3000' :
`file://${path.join(__dirname, '../build/index.html')}`
mainWindow.loadURL(appUrl)
mainWindow.maximize()
mainWindow.setFullScreen(true)
mainWindow.on('closed', () => mainWindow = null)
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
// Follow OS convention on whether to quit app when
// all windows are closed.
if (process.platform !== 'darwin') { app.quit() }
})
app.on('activate', () => {
// If the app is still open, but no windows are open,
// create one when the app comes into focus.
if (mainWindow === null) { createWindow() }
})
package.json
file:
"scripts"
section, add the following scripts:
"electron-dev": "concurrently \"BROWSER=none npm start\" \"wait-on http://localhost:3000 && electron .\"",
"electron-package": "./node_modules/.bin/electron-builder -c.extraMetadata.main=build/start-electron.js",
"preelectron-package": "npm run build"
These scripts will allow you to start electron in development mode (npm run electron-dev
), and to build (“package”) your application into an executable for the system you’re running on (npm run electron-package
).
"homepage"
value to read: "homepage": "./",
. (If it’s not there, add it.)"build"
section to the file:
"build": {
"appId": "com.loyno-mathcs.your-github-name.weatherstation.wotdevice",
"files": [ "build/**/*", "node_modules/**/*" ],
"directories":{
"buildResources": "assets"
}
},
(Be sure to replace your-github-name
above with your actual Github username.)
"name"
value to something more appropriate for this application (i.e., "name": "wot-device"
or similar).npm run electron-dev
You should see your app launch as a desktop application in a new window, and immediately become fullscreen.
npm run electron-package
You should find a new dist
directory with a folder for your operating system. Inside, you should see a new executable. Double-click it to test executing the app you’ve created.