简体   繁体   中英

How to find what node version a project supports

Is there a way besides trial and error to detect what node version I should use on a repository ?

With the fast rise of web frameworks it's becoming a common need to go back to projects from 6, 12 or 24+ months ago. I've been doing this a lot in the last weeks and my process for detecting the correct node version has become:

git clone [REPO]
npm i
[build]
### if error
rm -r node_modules
nvm use 4, 5 or 6
npm i
[build]

I can't help but feel that there's something very basic I'm missing here. Thanks for any wisdom you can share with me!

As mentioned in other answers, some packages have an engines field in the package.json . npm will warn users if they try to install a package on a Node.js version not supported by the package. The engines field looks like this:

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

More info: https://docs.npmjs.com/files/package.json#engines


Also, many packages have a CI, like TravisCI, set up to automatically test packages. These CI config files often contain a list of Node.js versions to test on. Generally, the lowest version number in the CI config is the minimum version supported by the package.

Common config file names are .travis.yml , appveyor.yml , circle.yml , etc.

Example:

.travis.yml :

language: node_js
node_js:
  - "4"
  - "5"
  - "6"
  - "7"

This config means that the package probably supports Node.js v4+

If these are your own repos, then you can store the Node version in package json for your reference.

"version": "1.0.0",
"engines": {
    "node": "7.x"
},
"description": "..."

This won't automatically setup the correct version, but it would provide you with somewhere to look.

if you do need to trial-and-error, you can speed up the process with npm's node combined with npx

npx node@4 myscript.js

there's even a shell autofallback option you can setup so just

node@4 myscript.js

项目可能有一个包含有效版本的.nvmrc文件

If the node engine parameter is not specified by the developer in package.json you can try looking at the same parameter in the package.json of your node modules. Chances are if your using common well support modules it will be defined. Running the following command will give you a semi sorted version list to go through.

grep -hoP '"node":.*' node_modules/*/package.json | sort

Usually the module that requires the highest version of node is determining factor in what version of node you'll need to for the project to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM