本文介绍了如何在 package.json 中指定所需的 Node.js 版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要 Node 12 或更高版本的 Node.js 项目.有没有办法在 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 }

package.json 文件的文档可以在 在 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 文件相同级别)创建一个 .npmrc 文件,文本为 engine-strict=true 将在安装过程中强制出错.

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 版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 07:56