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

后端 未结 4 2169
北荒
北荒 2021-02-07 06:35

I am searching for a way to break the build, if a user is using a different node.js version as defined in the project.

Ideally to put some checks in grunt or bower or np

4条回答
  •  情歌与酒
    2021-02-07 07:06

    "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)
    

提交回复
热议问题