如何为项目指定本地版本的 Node?

2022-01-15 00:00:00 node.js dependencies javascript npm

我有一个项目,我们试图让 Node 启动并在多个开发人员的机器上运行.问题在于,并非所有开发人员都是 Node(甚至 JavaScript)开发人员,我们希望确保他们拥有运行特定项目所需的 Node 版本(开发人员将在他们的机器上拥有多个 Node 项目).

I've got a project where we're trying to get Node up and running across multiple developers' machines. The problem is that not all of the developers are Node (or even JavaScript) developers, and we want to ensure that they have the Node version necessary to run a specific project (developers will have multiple Node projects on their machines).

我读到了 package.json 的engines"字段,但我似乎找不到任何方法来安装我需要的 Node 版本.为了测试,我通过 NVM 将我当前的节点版本设置为 v0.10.29,创建了一个 package.json 指定 v0.11.13 的必要引擎,并尝试通过 node 命令以及通过一个 package.json 定义的 npm start 命令.

I read about package.json's "engines" field, but I couldn't seem to find any way to get the version of Node installed that I needed. To test, I set my current node version to v0.10.29 via NVM, created a package.json specifying a necessary engine of v0.11.13, and tried to start Node via the node command as well as via a package.json-defined npm start command.

blackjack:node-engines-test sent1nel$ node -v
v0.10.29
blackjack:node-engines-test sent1nel$ cat package.json
{
  "name": "node-engines-test",
  "version": "0.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "engineStrict": true,
  "engines": {
    "node": "v0.11.13"
  },
  "start": "node index.js",
  "author": "",
  "license": "ISC"
}
blackjack:node-engines-test sent1nel$ cat index.js
console.log('Version: ' + process.version);
blackjack:node-engines-test sent1nel$ node index.js
Version: v0.10.29
blackjack:node-engines-test sent1nel$ npm start
blackjack:node-engines-test sent1nel$

npm install 似乎也不关心节点引擎版本.

npm install doesn't seem to care about the node engine version either.

blackjack:node-engines-test sent1nel$ npm install
npm WARN package.json node-engines-test@0.0.0 No description
npm WARN package.json node-engines-test@0.0.0 No repository field.
npm WARN package.json node-engines-test@0.0.0 No README data
blackjack:node-engines-test sent1nel$ node -v
v0.10.29

什么给了?!

推荐答案

我相信engines 和 engineStrict 用于包被 已安装(通过 npm),而不是在您尝试使用 node.执行 某些东西时.这些选项警告/阻止用户安装不适合(或兼容)他们当前使用的节点版本的包.

I believe that the engines and engineStrict are for when the package is being installed (via npm), not when you're trying to execute something with node. These options warn/prevent users from installing a package that is not designed to work (or compatible) with the node version they are currently using.

相关文章