简体   繁体   English

如何查找项目支持的节点版本

[英]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.随着 Web 框架的快速兴起,回到 6、12 或 24 个月前的项目已经成为一种普遍的需求。 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 .正如其他答案中提到的,一些包在package.json有一个engines字段。 npm will warn users if they try to install a package on a Node.js version not supported by the package.如果用户尝试在包不支持的 Node.js 版本上安装包, npm会警告用户。 The engines field looks like this: engines字段如下所示:

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

More info: https://docs.npmjs.com/files/package.json#engines更多信息: https : //docs.npmjs.com/files/package.json#engines


Also, many packages have a CI, like TravisCI, set up to automatically test packages.此外,许多包都有一个 CI,如 TravisCI,设置为自动测试包。 These CI config files often contain a list of Node.js versions to test on.这些 CI 配置文件通常包含要测试的 Node.js 版本列表。 Generally, the lowest version number in the CI config is the minimum version supported by the package.通常,CI 配置中的最低版本号是包支持的最低版本。

Common config file names are .travis.yml , appveyor.yml , circle.yml , etc.常见的配置文件名有.travis.ymlappveyor.ymlcircle.yml等。

Example:例子:

.travis.yml : .travis.yml

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

This config means that the package probably supports Node.js v4+此配置意味着该包可能支持 Node.js v4+

If these are your own repos, then you can store the Node version in package json for your reference.如果这些是您自己的存储库,那么您可以将 Node 版本存储在包 json 中以供参考。

"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如果您确实需要反复试验,则可以使用 npm 的节点与 npx 结合来加速该过程

npx node@4 myscript.js

there's even a shell autofallback option you can setup so just您甚至可以设置一个 shell autofallback 选项,所以只需

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.如果开发人员未在 package.json 中指定节点引擎参数,您可以尝试查看节点模块的 package.json 中的相同参数。 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.通常,需要最高版本节点的模块决定了项目运行所需的节点版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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