Prevent npm install for not supported Node.js versions

Published at
Updated at
Reading time
1 min

This post is part of my Today I learned series in which I share all my learnings regarding web development.

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.

Related Topics

See null comment.