且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在package.json中指定所需的Node.js版本?

更新时间:2022-12-13 09:11:29

我认为您可以使用引擎"字段:

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 }

可在npmjs网站上找到

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.