0

我知道可以使用 NVM、n 或类似方法在不同版本的 Node 之间切换。

在给定包中运行命令时,是否有一种方便的方法可以自动使用正确版本的 Node?(“正确版本”由engine标签或类似内容确定)。

例如,我希望能够做到这一点:

cd mypackage-that-needs-node10
npm run serve
# ... node 10 is used
cd ..
cd mypackage-that-needs-node14
npm run serve
# ... node 14 is used
4

2 回答 2

1

n支持engine从 中查找 Node.js 版本的标签package.jsonauto其中包含更多源,例如.node-version文件。

例如:

$ n install engine
# or run the target version of node
$ n run auto index.js
# or execute a command with that Node.js in path
$ n exec auto npm run serve
于 2021-08-08T11:08:12.520 回答
0

一种可能的方法是将节点本身安装到您的包中,并让npm run脚本优先使用它而不是节点的系统版本。见节点

于 2021-08-08T11:11:37.163 回答