Prevent npm install for not supported Node.js versions (#tilPost)

Yesterday I was reviewing a pull request to Contentful’s Gatsby starter and learned a nifty little detail about npm configurations.
The goal of his pull request was to guarantee that people setting up the project use a specific Node…

Yesterday I was reviewing a pull request to Contentful's Gatsby starter and learned a nifty little detail about npm configurations.

The goal of his pull request was to guarantee that people setting up the project use a specific Node.js version. You can define the engines property in your package.son to specify a version range.

{
  "engines": {
    "node": ">=12.0.0"
  }
}

A lot of projects define the property, but so far, I've never seen more than a little warning popping up when you run npm install. It turns out you can add a local npm configuration file (.npmrc) to your module/project root and explicitly turn on strict engine handling.

engine-strict=true

With this .npmrc in the project root, people are not able to run npm install if their Node.js is not fulfilling your requirements. 🎉

npm install
npm ERR! code ENOTSUP
npm ERR! notsup Unsupported engine for my-node-project@1.0.0: wanted: {"node":">=12.0"} (current: {"node":"10.20.1","npm":"6.14.4"})

FAIL 1

What about Yarn? Yarn doesn't need an additional configuration file and treats the engines property strictly by default.


Reply to Stefan
Print Share Comment Cite Upload Translate
CITATION GOES HERE CITATION GOES HERE
Select a language: