如何在package.json中指定所需的Node.js版本? [英] How can I specify the required Node.js version in package.json?
问题描述
我有一个Node.js项目,需要Node版本12或更高版本.是否可以在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 will automatically check and inform the users if they need to upgrade?
推荐答案
我认为您可以使用引擎"字段:
I think you can use the "engines" field:
{ "engines" : { "node" : ">=0.12" } }
正如您所说的那样,您的代码绝对不适用于任何较低版本,您可能也希望使用"engineStrict"标志:
As you're saying your code definitely won't work with any lower versions, you probably want the "engineStrict" flag too:
{ "engineStrict" : true }
Documentation for the package.json file can be found on the npmjs site
更新
engineStrict
现在已弃用,因此这只会发出警告.现在,用户可以根据需要运行npm config set engine-strict true
.
engineStrict
is now deprecated, so this will only give a warning. It's now down to the user to run npm config set engine-strict true
if they want this.
更新2
如下文所指出的,如果节点版本不是,则在项目的根目录(与package.json文件处于同一级别)上创建文本为engine-strict=true
的.npmrc
文件将在安装过程中产生错误.兼容.
As ben pointed out below, creating a .npmrc
file at the root of your project (the same level as your package.json file) with the text engine-strict=true
will force an error during installation if the Node version is not compatible.
这篇关于如何在package.json中指定所需的Node.js版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!