37

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 npm to stop, if a certain npm/node version is not used to run the current build.

cilap
  • 2,215
  • 1
  • 25
  • 51

5 Answers5

29

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.

blimmer
  • 2,038
  • 20
  • 23
  • For yarn berry (v2+) users, I recommend using the [yarn-plugin-engines](https://github.com/devoto13/yarn-plugin-engines) plugin. Also upvote https://github.com/yarnpkg/berry/issues/1177 for native support. – blimmer Oct 28 '22 at 18:39
9

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

Bhushankumar Lilapara
  • 780
  • 1
  • 13
  • 26
Ben A.
  • 480
  • 2
  • 7
  • ben, thx. engineStrict and engine solved my issue. Placed your answer as anwered – cilap Feb 09 '15 at 16:06
  • 4
    engineStrict is getting deprecated in the v3 of npm, do you have a new suggestion? – cilap Jun 10 '15 at 15:05
  • 4
    @cilap Since `engineStrict` has been deprecated, you should use you should use `--engine-strict=true` as an argument to `npm` in conjunction with the `engines` parameter in the `package.json` – Nayan Hajratwala Jan 16 '18 at 16:37
  • 7
    You can also put `engine-strict=true` in your `.npmrc` file to enforce it for the project. – Joshua Pinter Aug 03 '19 at 04:30
9

"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)
Yair Kukielka
  • 10,686
  • 1
  • 38
  • 46
  • I upvoted this answer because I needed a solution that would enforce even after installation. I needed to enforce Node 16 because Node 18 doesn't work at the moment. I wanted to prevent someone installed my project using 16 and then upgrading. I decided to write a shell script instead of using the JS code above. – aakoch Nov 26 '22 at 21:10
6

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"
    }
}
Abbas Gadhia
  • 14,532
  • 10
  • 61
  • 73
1

If you want to enforce a specific version of npm, you might use: https://github.com/hansl/npm-enforce-version

If you want to enforce a version of node when executing you can read the version of node that is currently running by checking against:

process.versions

For more info: https://nodejs.org/api/process.html#process_process_versions

eremzeit
  • 4,055
  • 3
  • 30
  • 32
  • 1
    That npm-enforce-version package hasn't been updated since 2015. And since it tries to enforce the npm version to match its own package version, it can't work for any version of npm > 2.1.11. – Lachlan Hunt May 06 '19 at 06:58