Verify a Specific Node Version for Your JavaScript Project

Problem

I recently had a situation where I needed to make sure the correct version of node is installed before one runs npm install.
This is because one of the packages in our project has a dependency on a specific node version.

Another challenge was that I should still be able to work on multiple projects, each with their own node version dependency.

My mission is clear, let the games begin…

Solution

nvm

To address the issue of running multiple versions of node on one machine, use nvm (Node Version Manager).

nvm is a version manager for node.js, designed to be installed per-user, and invoked per-shell.

I needed a windows version, and the nvm Readme.md pointed me to nvm-windows (Download nvm-setup.zip).

Similar (not identical) to nvm, but for Windows.

check-node-version

To verify the node version per JavaScript project, I found this npm package check-node-version.
It allows you to verify the node version via CLI (Command Line Interface) or programmatically.
I used both ways, just because I had two projects with different requirements.


Verify node version via CLI

I used this approach on a project where the project could not even install packages if you had an incorrect node version.
I had to check the node version without having the luxury of npm install.

How do one use a npm package without executing npm install? … npx

This command allows you to run an arbitrary command from an npm package (either one installed locally, or fetched remotely), in a similar context as running it via npm run.

Steps to check node version between v10.x (including) and v11.0.0 (excluding)
  1. use engines in your package.json to add a specific node version, or a range, to your project.

package.json:

1
2
3
4
5
6
7
8
9
10
11
12
{
...
"name": "fantastic app",
"version": "0.0.1",
"engines": {
"node": ">=10.12.0 <11.0.0"
},
"scripts": {
...
}
...
}
  1. add a check_node_version.js to your project.

check_node_version.js: An example of the basic version check…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const { exec } = require("child_process");
const { engines } = require('./package');

exec("npx check-node-version --package", (error, stdout) => {
const requiredNodeVersion = engines.node;
if (error) {
console.error(error);
return;
}

if (result.isSatisfied) {
console.log("All is well.");
return;
}

console.error("Some package version(s) failed!");

for (const packageName of Object.keys(result.versions)) {
if (!result.versions[packageName].isSatisfied) {
console.error(`Missing ${packageName}.`);
}
}
}

With the knowledge of nvm, I’ve update check_node_version.js with some instruction to install and use nvm.
Give your dev team instructions on how to solve the problem, they will love you for it. 😁

check_node_version.js: An example of the version check with instructions to solve the issue for windows users…

  1. update package.json with preinstall script…
1
2
3
4
5
6
7
8
9
10
{
...
"scripts": {
...
"requirements-check": "node check_node_version.js",
"preinstall": "npm run requirements-check"
...
}
...
}

When you run npm install, the preinstall will execute requirements-check, and when all is good it will continue installing the project packages. If the node verification failed, the packages will not be installed.


Verify node version programmatically

  1. install check-node-version as a dev dependency:
1
npm install check-node-version -D
  1. use engines in your package.json to add a specific node version, or a range, to your project.

package.json:

1
2
3
4
5
6
7
8
9
10
11
12
{
...
"name": "other fantastic app",
"version": "0.0.1",
"engines": {
"node": ">=12.13.0"
},
"scripts": {
...
}
...
}
  1. add a check_node_version.js to your project.
  1. update package.json with postinstall script…
1
2
3
4
5
6
7
8
9
10
{
...
"scripts": {
...
"requirements-check": "node check_node_version.js",
"postinstall": "npm run requirements-check",
...
}
...
}

When you run npm install, your packages will be installed first, and after that postinstall will execute requirements-check to verify the correct node version.

One dream I have to even make this better, is to show the instructions with an added question:
Would you like to download nvm and install the missing node version? [Y]es / [N]o.
(There is some homework for you )

Final thoughts

When you add any breaking changes to your codebase, I believe it is your responsibility to help your teammates to fall into the pit of success without any cognitive overload.

Use it…don’t use it :)

Kudos