I was wondering if I could port my application, built with NW.JS 0.94.0 to ElectronJS 34.0.2. I found this workaround, that is quite ugly, but seems to be somewhat fully working.
First, you'll want to follow the ElectronJS Tutorial to build a new app. Mainly adding to devDependencies the electron
package. Remove, of course, everything related to NWJS.
In ./package.json
:
"main": "app/main.js",
"scripts": {
"start": "electron ."
},
In ./app/main.js
:
const { app, BrowserWindow } = require('electron/main')
const createWindow = () => {
const win = new BrowserWindow({
width: 1024,
height: 650,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
win.loadFile('app/index.html')
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
This will, thanks to nodeIntegration
, allow the ElectronJS Rendered to use node_modules, like NWJS does. Then running npm run start
would actually try to load my app.
There were a few things to consider:
const win = gui.Window.get()
wouldnt work, that's NW stuff. All reference to thosewin
orgui
need to go or be ported- the tray and context menu code has to go or be ported
require('package.json')
or other similar local files wouldn't work, but there's a workaround : usingrequire('../package.json')
insteadjQuery
and$
were undefined, because they were imported as a script in the main HTML file. This fixed it:
<script src="js/vendor/jquery.min.js" type="application/javascript"></script>
<script>window.$ = window.jQuery = require('../app/js/vendor/jquery.min.js')</script>