Skip to content

Instantly share code, notes, and snippets.

@vankasteelj
Created February 4, 2025 22:19
Show Gist options
  • Save vankasteelj/4aeafc53949bc71950dedb6001741b4e to your computer and use it in GitHub Desktop.
Save vankasteelj/4aeafc53949bc71950dedb6001741b4e to your computer and use it in GitHub Desktop.
Port a NWJS app to Electron (as of feb.2025)

How to port a NW.JS application to ElectronJS

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 those win or gui 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 : using require('../package.json') instead
  • jQuery 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment