How to specify/enforce a specific node.js version to use in package.json?

You can use the engines property in the package.json file

For example, if you want to make sure that you have a minimum of node.js version 6.9 and a maximum of 6.10, then you can specify the following

package.json
{
    "name": "Foo",
    ....
    "engines": {
        "node": ">=6.9 <=6.10"
    }
}

Even though engineStrict is deprecated, you can still accomplish this behavior without needing to use an additional script to enforce a Node version in your project.

  1. Add the engines property to your package.json file. For example:

    {
      "name": "example",
      "version": "1.0.0",
      "engines": {
       "node": ">=14.0.0"
      }
    }
    
  2. Create a .npmrc file in your project at the same level as your package.json.

  3. In the newly created .npmrc file, add engine-strict=true.

    engine-strict=true
    

This will enforce the engines you've defined when the user runs npm install. I've created a simple example on GitHub for your reference.


You can use the "engineStrict" property in your package.json

Check the docs for more information: https://docs.npmjs.com/files/package.json

Update on 23rd June 2019

"engineStrict" property is removed in npm 3.0.0.

Reference : https://docs.npmjs.com/files/package.json#enginestrict


"engineStrict" has been removed and "engines" only works for dependencies. If you want to check Node's runtime version this can work for you:

You call this function in your server side code. It uses a regex to check Node's runtime version using eremzeit's response It will throw an error if it's not using the appropriate version:

const checkNodeVersion = version => {
  const versionRegex = new RegExp(`^${version}\\..*`);
  const versionCorrect = process.versions.node.match(versionRegex);
  if (!versionCorrect) {
    throw Error(
      `Running on wrong Nodejs version. Please upgrade the node runtime to version ${version}`
    );
  }
};

usage:

checkNodeVersion(8)

Tags:

Node.Js

Npm