If a Node.js version is specified compatibility issues can be avoided.
Posted
Updated
Europe’s developer-focused job platform
Let companies apply to you
Developer-focused, salary and tech stack upfront.
Just one profile, no job applications!
This article is based on Node v18.13.0
Specifying a Node.js version is important, to reduce issues, especially when working in a big team. There are many more factors to consider, like:
💰 The Pragmatic Programmer: journey to mastery. 💰 One of the best books in software development, sold over 200,000 times.
Your Single-Page-Application or Node.js Application should be stable, compatible and reliable. Hence, specify the Node.js version and save yourself some headaches.
There are several ways to specify a version. You can add a configuration file for NVM or extend package.json
or you write a custom script.
If you installed node with NVM, how to install node.js with NVM, you can simply add a configuration file at the root of your project. This file will also be checked into git, so you can track it.
.nvmrc
at the root of your project.touch .nvmrc
Add the required Node.js version to it.
If you are already using the required version
node -v > .nvmrc
Otherwise, you can specify it directly in the file, use a text editor of your choice
nvm use
command to tell NVM to use node version in the configuration file.
After using the command the output should be similar to this.Found '.nvmrc' with version <v18.13.0>
Now using node v18.13.0 (npm v8.19.3)
This approach will not create a warning, when installing dependencies. You have to update your README
and inform your team members about the change.
To specify a Node.js version, you can simply add an engines
field in your package.json
.
See NPM docs.
Until NPMv3, it was possible to create an .npmrc
file to enable engine-strict=true
so that it would throw an error
when installing dependencies while being on the wrong node.js version. Now, it will not throw an error, but show in the console a warning.
Open your package.json
, add the "engines"
field and specify the node
version.
You can use semver to include version ranges or specific limits.
{
"engines": {
"node": ">=16.0.0 <17.0.0"
}
}
Unfortunately, this will only create warning when installing the dependencies. Hence, you have to update your README
and inform your team members about the change.
A good approach would be to combine setting the engine
field in your package.json
and check if the current node version is within the version range.
To do this we can write a simple script in five steps.
{
"engines": {
"node": ">=18.0.0"
}
}
engineStrict
field to your package.json and set it to true.
This field will be read in our custom script. If it's true the script will be executed.{
"engineStrict": true
}
semver
and create a file check-node-version.js
.npm i semver
touch check-node-version.js
semver
package.const semver = require('semver');
const engines = require('./package').engines;
const version = engines.node;
if (!semver.satisfies(process.version, version)) {
console.log(
`Required node version ${version} not satisfied with current version ${process.version}.`,
);
process.exit(1);
}
package.json
with a preinstall
step and a check-node-version
script.{
"check-node-version": "node check-node-version.js",
"preinstall": "npm run check-node-version"
}
And that's it. If you run npm i
in your project, and you are on different Node version than specified, the process will exit.
All three approaches from above can be used in React or Angular.
.nvmrc
at the root of your project, specify version and use with nvm use
.engines
field in package.json
and specify Node.js version.engines
field with Node process and exit if it's not within specified semver range.Thanks for reading and if you have any questions, use the comment function or send me a message @mariokandut.
If you want to know more about Node, have a look at these Node Tutorials.
Never miss an article.