How to specify / apply a specific version of node.js for use in package.json?

I am looking for a way to break an assembly if the user uses a different version of node.js as defined in the project.

Ideally, put some checks in grunt or bower or npm to stop if a specific version of npm / node is not used to start the current build.

+8
source share
4 answers

You can use the engineStrict property in your package.json

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

June 23, 2019 Patch

"engineStrict" removed in npm 3.0.0.

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

+3

engines package.json

, , node.js 6.9 6.10,

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

"engineStrict" , "" . Node, :

You call this function in your server code. It uses a regular expression to check the version of Node using the eremzeit response. It throws an error if it does not use the corresponding 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}`
    );
  }
};

using:

checkNodeVersion(8)
0
source

All Articles