A quick run down of these two technologies, why they are great individually and how best to use them together.
NW.js
NW.js is a runtime environment for building Cross-Platform Desktop Apps (XPDAs). It is essentially a Chromium Browser with Node.js built in. Similar to Electron, but actually good!
- It allows you to package your application with protected (native binary) source code (closed source) (not possible in Electron).
- It also has a ton of options around manipulating the window itself, including Kiosk mode (full screen, can’t escape/close), frameless window, transparent window, hidden window, tray apps, etc.
- NW.js allows full access to the entire Node API and any Node Module directly from the DOM.
- Runs on all platforms, even as far back as Windows XP and OSX 10.6, but also on all the new stuff, including Apple Silicon.
Volta
Volta is a Node Version Manager with out all the manual steps. There are many alternative options to solve the same problem, but honestly they all kinda suck in comparison. Let’s break down what it does so well:
- Works on all desktop OS’s.
- It is the only option that runs on all platforms. Which means you have one system, one set of steps, for everyone that contributes to a project.
- I can’t stress this enough, it is the best option on every platform. It even runs on Windows 7, which is nuts. But also everything else.
- All the other Node Version Managers require you to manually run a command to install a Node or npm version, and they also require you to run a command to switch between Node versions. With Volta, you just write down the version your project needs in the
package.json
and you’re done. Volta will automatically switch versions if you run a Node command in a repo that has Node version specifed in thepackage.json
. - Most other options only handle Node, or maybe Node and npm (usually in a clunky way). Volta works with Node, npm, yarn, pnpm, whatever.
- A lot of other Node Version Managers have issues where you install them, and they work, then you close the terminal and reopen it and they are just gone and you need to re-install them. Volta doesn’t have this issue, it just works, it’s awesome.
Why combine them?
When you run npm install
some dependencies may download or build files that are specific to your global Node version. If you change your Node version, they will need to be re-installed to get a new matching dependency.
Since NW.js has it’s own version of Node built in, that means when you run npm install
your global Node version needs to match what is built in to NW.js so that the dependencies will be compatible with it.
How to combine them?
I just made a new Node library to make this process as simple as possible.
- Remove any Node Version Managers you already have installed
- Go to https://volta.sh and install Volta
- In your NW.js project, run this command:
- npm pkg set scripts.postinstall=”npx base-volta-off-of-nwjs”
- Run
npm install
That’s it, your package.json
will go from this:
{
"name": "your-app-name",
"main": "index.html",
"devDependencies": {
"nw": "^0.82.0"
},
"scripts": {
"start": "nw ."
}
}
to this:
{
"name": "your-app-name",
"main": "index.html",
"devDependencies": {
"nw": "^0.82.0"
},
"scripts": {
"start": "nw .",
"postinstall": "npx base-volta-off-of-nwjs"
},
"volta": {
"node": "20.7.0"
}
}
And any time you run npm install
it will always check to make sure your Node version matches NW.js’s Node version.
You can even use Volta in GitHub Actions to ensure your CI is always using the correct Node version. Here is an example:
name: Node.js CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: volta-cli/action@v4
- run: npm ci
- run: npm run lint
- run: npm t