Force correct Node.js version with npm

Fabian Illner
1 min readApr 21, 2020

--

unsplash.com

You want everybody in your project to use a specific node version. One of the possibilities to achieve this, is by restricting it with configuration. This can be done with just two steps:

  • set engines in package.json
  • enforce with engine-strict per project

In this example we want at minimum of node version 12:

Set engines at root level in package.json:

{
...
"engines": {
"node": ">=12"
},
...
}

So now we tell everybody using this project (or package) which node version is required. For more info on how to define more specific versions have a look at: https://docs.npmjs.com/files/package.json#engines

Next step is to enforce the version in node. This can be achieved by an npm config called engine-strict. We can set this per project with an .npmrc file which should be created next to package.json.

Inside .npmrc:

engine-strict=true

https://docs.npmjs.com/misc/config#engine-strict

And that’s it! :)
You should get an error when you try to execute e.g. npm run start .

--

--