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)
- use engines in your
package.json
to add a specific node version, or a range, to your project.
package.json
:
1 | { |
- add a
check_node_version.js
to your project.
check_node_version.js
: An example of the basic version check…
1 | const { exec } = require("child_process"); |
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…
- update
package.json
withpreinstall
script…
1 | { |
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
- install
check-node-version
as a dev dependency:
1 | npm install check-node-version -D |
- use engines in your
package.json
to add a specific node version, or a range, to your project.
package.json
:
1 | { |
- add a
check_node_version.js
to your project.
- update
package.json
withpostinstall
script…
1 | { |
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 :)