Did you ever bounced upon some versioning problems while using npm and nodejs? There is a simple solution to fix it and it also allow you to be more strict about your versions. And as a bonus: if you didn't specify one of these versions in github actions, it will now start using them as well. Great to avoid random failing builds!
Setup Engines 🔗
Inside your package.json file, you can define your versions. Simply add the following lines to do so:
"engines": {
"node": ">=20.0.0",
"npm": ">=10.0.0"
}
As you can see, it is pretty straightforward. You can utilize this approach by using semantic versioning as prefered, forcing everyone to stay at the configured version(s).
And last but not least, we can also throw up an error if a wrong version is used. Inside your .npmrc (it should be at the same directory as your package.json file), add the following line:
engine-strict=true
Running npm install will now return that a specific version is expected instead of what is being used (in case of a wrong version). As an example, I've set my required node version to 19.0.0 and npm to 8.0.0. And as we may see, the error is thrown:
I hope this was helpful!