如何在 package.json 中指定所需的 Node.js 版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29349684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I specify the required Node.js version in package.json?
提问by Erel Segal-Halevi
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 will automatically check and inform the users if they need to upgrade?
我有一个需要 Node 12 或更高版本的 Node.js 项目。有没有办法在packages.json文件中指定这个,这样安装程序会自动检查并通知用户是否需要升级?
回答by IBam
I think you can use the "engines" field:
我认为您可以使用“引擎”字段:
{ "engines" : { "node" : ">=0.12" } }
As you're saying your code definitely won't work with any lower versions, you probably want the "engineStrict" flag too:
正如您所说,您的代码肯定不适用于任何较低版本,您可能也需要“engineStrict”标志:
{ "engineStrict" : true }
Documentation for the package.json file can be found on the npmjs site
package.json 文件的文档可以在 npmjs 站点上找到
Update
更新
engineStrictis now deprecated, so this will only give a warning. It's now down to the user to run npm config set engine-strict trueif they want this.
engineStrict现在已弃用,因此这只会发出警告。现在取决于用户npm config set engine-strict true是否需要运行。
Update 2
更新 2
As ben pointed out below, creating a .npmrcfile at the root of your project (the same level as your package.json file) with the text engine-strict=truewill force an error during installation if the Node version is not compatible.
正如本下面指出的,如果 Node 版本不兼容.npmrc,在项目的根目录(与 package.json 文件相同级别)创建一个带有文本的文件engine-strict=true将在安装过程中强制出错。
回答by Mikel
Add
添加
to package.json
到 package.json
"engines": {
"node": ">=10.0.0",
"npm": ">=6.0.0"
},
to the file .npmrc(close to package.json, same directory)
到文件.npmrc(接近package.json,同一目录)
engine-strict=true
回答by Adam
Just like said Ibam, engineStrictis now deprecated. But I've found this solution:
就像 Ibam 所说的那样,engineStrict现在已弃用。但我找到了这个解决方案:
check-version.js:
检查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:
包.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
在此处了解更多信息:https: //medium.com/@adambisek/how-to-check-minimum-required-node-js-version-4a78a8855a0f#.3oslqmig4
.nvmrc
.nvmrc
And one more thing. A dotfile '.nvmrc' can be used for requiring specific node version - https://github.com/creationix/nvm#nvmrc
还有一件事情。dotfile '.nvmrc' 可用于要求特定的节点版本 - https://github.com/creationix/nvm#nvmrc
But, it is only respected by npm scripts (and yarn scripts).
但是,它只受到 npm 脚本(和纱线脚本)的尊重。
回答by vnglst
There's another, simpler way to do this:
还有另一种更简单的方法来做到这一点:
npm install Node@8(saves Node 8 as dependency in package.json)- Your app will run using Node 8 for anyone- even Yarn users!
npm install Node@8(将 Node 8 保存为 package.json 中的依赖项)- 您的应用程序将使用 Node 8 为任何人运行- 甚至是 Yarn 用户!
This works because nodeis just a package that ships node as its package binary. It just includes as node_module/.bin which means it only makes node available to package scripts. Not main shell.
这是有效的,因为node它只是一个将节点作为其包二进制文件发送的包。它只包含为 node_module/.bin 这意味着它只使 node 可用于打包脚本。不是主壳。
See discussion on Twitter here: https://twitter.com/housecor/status/962347301456015360
在此处查看 Twitter 上的讨论:https: //twitter.com/housecor/status/962347301456015360
回答by Jamie Nicholl-Shelley
A Mocha test case example:
Mocha 测试用例示例:
describe('Check version of node', function () {
it('Should test version assert', async function () {
var version = process.version;
var check = parseFloat(version.substr(1,version.length)) > 12.0;
console.log("version: "+version);
console.log("check: " +check);
assert.equal(check, true);
});});

