How can I specify the required version of Node.js in packages.json?

I have a Node.js project that requires Node version 12 or higher. Is there a way to specify this in the packages.json file so that the installer automatically checks and informs users about the need for updates?

+117
npm version packages
Mar 30 '15 at 15:06
source share
5 answers

I think you can use the "engines" field:

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

Since you say that your code will definitely not work with any lower versions, you probably also want the "engineStrict" flag:

 { "engineStrict" : true } 

The documentation for the package.json file can be found at npmjs.

Refresh

engineStrict is no longer recommended, so this will only be a warning. Now the user can run npm config set engine-strict true if he wants to.

+142
Mar 30 '15 at 15:09
source share

As Ibam said, engineStrict now deprecated. But I found this solution:

check-version.js:

 import semver from 'semver'; import { engines } from './package'; const version = engines.node; if (!semver.satisfies(process.version, version)) { console.log(`Required node version ${version} not satisfied with current version ${process.version}.`); process.exit(1); } 

package.json:

 { "name": "my package", "engines": { "node": ">=50.9" // intentionally so big version number }, "scripts": { "requirements-check": "babel-node check-version.js", "postinstall": "npm run requirements-check" } } 

Find out more here: https://medium.com/@adambisek/how-to-check-minimum-required-node-js-version-4a78a8855a0f#.3oslqmig4

.nvmrc

And one more thing ... Dotfile ".nvmrc" can be used to use a specific version of node (but I have not tried it yet) - https://github.com/creationix/nvm#nvmrc

+31
Jan 12 '17 at 18:55
source share

add

in package.json

  "engines": { "node": ">=10.0.0", "npm": ">=6.0.0" }, 

to the .npmrc file (next to package.json , same directory)

 engine-strict=true 
+30
Jun 11 '18 at 13:13
source share

There is another, easier way to do this:

  1. npm install Node@8 (saves Node 8 as a dependency in package.json)
  2. Your app will work with Node 8 for everyone - even for Yarn users!

This works because node is just a package that sends the node as its binary package. It just includes as node_module / .bin, which means that it makes the node available only to package scripts. Not the main shell.

See the discussion on Twitter here: https://twitter.com/housecor/status/962347301456015360

+2
Feb 11 '18 at 13:45
source share

.nvmrc

If you use NVM like this , which you probably should, then you can specify the version of nodejs needed for this project in the .nvmrc file .nvmrc tracking .nvmrc :

 echo v10.15.1 > .nvmrc 

This does not automatically take effect on cd , which is normal: the user must then do:

 nvm use 

and now this version of the node will be used for the current shell.

You can list the versions of nodes that you have:

 nvm list 

.nvmrc documented at: https://github.com/creationix/nvm/tree/02997b0753f66c9790c6016ed022ed2072c22603#nvmrc

Tested with NVM 0.33.11.

0
Feb 03 '19 at 21:17
source share



All Articles