Volta and NW.js are amazing together
Tags: javascript, volta, nwjs, desktop
Published: 2023-12-14 18:17:14

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).
1. 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.
1. NW.js allows full access to the entire Node API and any Node Module directly from the DOM.
1. 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:

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

1. Remove any Node Version Managers you already have installed
1. Go to https://volta.sh and install Volta
1. In your NW.js project, run this command:
   * `npm pkg set scripts.postinstall="npx base-volta-off-of-nwjs@latest"`
1. Run `npm install`

That's it, your `package.json` will go from this:

```json
{
  "name": "your-app-name",
  "main": "index.html",
  "devDependencies": {
    "nw": "^0.82.0"
  },
  "scripts": {
    "start": "nw ."
  }
}
```

to this:

```json
{
  "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"
  }
}
```

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:

```yml
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
```

* * *

## 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.
      1. 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.
1. **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.
      1. 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.

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:

* https://github.com/volta-cli/volta/issues/1523#issuecomment-1908236258