DEV Community

The Jared Wilcurt
The Jared Wilcurt

Posted on • Edited on

Volta and NW.js are amazing together

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!

  1. It allows you to package your application with protected (native binary) source code (closed source) (not possible in Electron).
  2. 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.
  3. NW.js allows full access to the entire Node API and any Node Module directly from the DOM.
  4. 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", but without 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:

  1. 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.
  2. 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 specified in the package.json.
  3. Most other options only handle Node, or maybe Node and npm (usually in a clunky way). Volta works with Node, npm, yarn, pnpm, whatever.
  4. 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 its 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.

  1. Remove any Node Version Managers you already have installed
  2. Go to https://volta.sh and install Volta
  3. In your NW.js project, run this command:
    • npm pkg set scripts.postinstall="npx base-volta-off-of-nwjs@latest"
  4. 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 ."
  }
}
Enter fullscreen mode Exit fullscreen mode

to this:

{
  "name": "your-app-name",
  "main": "index.html",
  "devDependencies": {
    "nw": "^0.82.0"
  },
  "scripts": {
    "start": "nw .",
    "postinstall": "npx base-volta-off-of-nwjs@latest"
  },
  "volta": {
    "node": "20.7.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Downsides

  1. Problem: If you are on a corporate VPN, then using Volta to download Node for you may have issues. This is because from the Node.js download server's perspective, everyone on your VPN using Volta to download Node will look like the same person. So Node may rate limit you.
    • Solutions:
      1. Just wait a little bit and try again, usually within a few minutes the rate limit goes away and you are fine to continue.
      2. Disconnect from the VPN and try again, then reconnect. You'll only ever download a specific Node version once, from then on you'll have it cached.
    • Personal Note: I started using Volta in October of 2023. I've ran into this twice in 2024 (Feb and July), and I saw my co-worker run into once. So it happens, but it's rare. And honestly compared to Volta's competitors I've still spent less time debugging it than any of them (again, all other options kinda suck).
  2. Problem: CI tool fails to download Volta. This is for the same reason as the VPN issue. All download requests coming from GitHub Actions will look the same to the Node.js download server, and therefor may get rate-limited.
    • Solutions:
      1. Just wait a few minutes and try again, should work fine.
      2. Change your CI config to not use Volta and instead hard-code your required Node version, which most systems will see and use a copy they already have on the image, or on an internal network, rather than downloading it from Node's server.
    • Personal Note: I've ran into this maybe 3 times in the past year. For CI's that are flaky already, I wouldn't use this, but for most of my repos it is a nice convenience. Comes down to picking which inconvenience you prefer: "Do I want to update the same value in two different files every time I update Node, or do I want to have to re-run GHA a few times a year". To me the answer is different for different repos.

To some these problems will not affect you at all. To others they may be deal breakers. But these issues can also be resolved by Volta falling back to a list of mirrors when being rate-limited by Node's download server. There is already an issue for it, which you can thumbs up to encourage getting this resolved sooner:

Top comments (0)