

How to prevent npm install for unsupported Node.js versions
source link: https://www.wisdomgeek.com/development/web-development/javascript/how-to-prevent-npm-install-for-unsupported-node-js-versions/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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"
}
}
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: '[email protected]',
npm WARN EBADENGINE required: { node: '<14.0.0' },
npm WARN EBADENGINE current: { node: 'v14.15.0', npm: '7.5.2' }
npm WARN EBADENGINE }
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
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: [email protected]
npm ERR! notsup Not compatible with your version of node/npm: [email protected]
npm ERR! notsup Required: {"node":"<14.0.0"}
npm ERR! notsup Actual: {"npm":"7.5.2","node":"v14.15.0"}
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 [email protected]: 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.
And that is all it takes to prevent npm install for unsupported Node.js versions! 🎉
Recommend
-
53
Prevent Composer from updating your Symfony components to unsupported versions with the colinodell/symfony-lts-or-current metapackage.
-
7
Unsupported versions of Windows and .NET Octopus Deploy Matthew Casperson June 22, 2021
-
10
Learn How to Install Npm and Node.js on Ubuntu By Wini Bhalla Published 11 hours ago Need to install Node.js and Npm through Ubunt...
-
11
The easy way to install Windows 11 on unsupported CPUs Microsoft’s own registry hack makes upgrades painless If you buy something from a...
-
5
Microsoft warns of an increased likelihood of bugs Windows 11 will finally become available for non-insider devices tomorrow, and this is clearly good news for everybody, though the updated system r...
-
7
This is how you can bypass TPM 2.0 and CPU checks Windows 11 is finally here, but for many users out there, this isn’t such a big deal simply because they’re stuck with Windows 10. M...
-
16
Welcome to our guide on how to install Latest Node.js and NPM on Ubuntu & Debian Linux distributions. Node.js is a free and open source server-side programming language which runs on various platforms (Linux, Windows, Unix, macOS). Node.j...
-
2
Install software or upgrade from an old unsupported release 205 views 1 month ago Ubuntu If you have old c...
-
7
<?xml encoding="utf-8" ??>Node.js & NPM are now available on Debian 11 Bullseye. Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It uses an event-driven, non-blocking I/O model...
-
10
How to Install Windows 11 in a Virtual Machine On Unsupported Hardware By Jason Currie Published 19 hours ago
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK