JavaScript

How to prevent npm install for unsupported Node.js versions

Advertisements

npm configurations allow us to do quite a lot of nifty things. One of them is to allow the project to set the Node.js version that needs to be used in order to run the project. This also provides us with the functionality to prevent npm install for unsupported Node.js versions.

The engines property

The engines property in the package.json can be used to define supported Node.js versions. It can accept a version range.

{
  "engines": {
    "node": ">=0.10.3 <14"
  }
}
JavaScript

Specifying this property does not enforce the version. It only shows a warning when the user runs npm install on an unsupported Node.js version:

$ npm install

npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'wisdom-geek@1.0.0',
npm WARN EBADENGINE   required: { node: '<14.0.0' },
npm WARN EBADENGINE   current: { node: 'v14.15.0', npm: '7.5.2' }
npm WARN EBADENGINE }
Bash

Stopping npm install with an unsupported Node.js version

We need to create an npm configuration in the root directory of our project. You might know about this file as the .npmrc file. We then need to explicitly specify that we want to turn on engine checking for the project by using the key-value pair:

engine-strict=true
JavaScript

Once this is specified, and if someone tries to do an npm install on an unsupported Node.js version, they will get an error:

 npm install

npm ERR! code EBADENGINE
npm ERR! engine Unsupported engine
npm ERR! engine Not compatible with your version of node/npm: wisdom-geek@1.0.0
npm ERR! notsup Not compatible with your version of node/npm: wisdom-geek@1.0.0
npm ERR! notsup Required: {"node":"<14.0.0"}
npm ERR! notsup Actual:   {"npm":"7.5.2","node":"v14.15.0"}
Bash

How do I do this while using Yarn?

Yarn does not need the .npmrc file and treats the engine property as strict by default.

$ yarn install

yarn install v1.22.5
info No lockfile found.
[1/5] 🔍  Validating package.json...
error wisdom-geek@1.0.0: The engine "node" is incompatible with this module. Expected version "<14.0.0". Got "14.15.0"
error Found incompatible module.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
Bash

And that is all it takes to prevent npm install for unsupported Node.js versions! 🎉

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

Share
Published by
Saransh Kataria

Recent Posts

Fixing cookies are blocked for a website with shields down on Brave

I recently switched completely to the Brave browser and have set ad blocking to aggressive…

4 months ago

Generating a QR code using Node.js

I was preparing a slide deck for a hackathon and decided to put in a…

5 months ago

How to clear the global npx cache

I have been using npx a lot lately, especially whenever I want to use a…

5 months ago

Copy/Pasting output from the terminal

Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…

6 months ago

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

7 months ago

Native popover API in HTML

Popovers have been a problem that was typically solved by using a third-party solution. But…

7 months ago
Advertisements