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…