我想在我的 NodeJS 项目中强制执行 NodeJS 和 NPM 的确切版本。这是我的 package.json:
{
"name": "nodesandbox",
"version": "0.0.1",
"dependencies": {
"less": "*"
},
"engines": {
"node": "0.10.25"
}
}
当我运行 nvm ls
时,我得到了这个输出:
v0.2.6
v0.7.12
v0.9.6
v0.10.12
v0.10.15
v0.10.21
current: v0.10.24
当我运行 npm install
时它工作正常,但我希望它失败。我已按照此链接 [1] 中的说明进行操作,这就是为什么我认为在引擎对象中设置 Node 版本应该有效,但遗憾的是它不起作用。
[1] http://blog.nodejitsu.com/package-dependencies-done-right/#node-dependency-in-apps
最佳答案
它看起来像 engine
仅当从另一个位置获取包时才会检查依赖项:
[timwolla@/tmp/test2]npm install ../test
npm WARN engine nodesandbox@0.0.1: wanted: {"node":"0.10.30"} (current: {"node":"v0.10.26","npm":"1.4.3"})
如果engineStrict
设置为真:
[timwolla@/tmp/test2]npm install ../test
npm ERR! notsup Unsupported
npm ERR! notsup Not compatible with your version of node/npm: nodesandbox@0.0.1
npm ERR! notsup Required: {"node":"0.10.30"}
npm ERR! notsup Actual: {"npm":"1.4.3","node":"v0.10.26"}
如果您阅读 the manual仔细看看为什么会很清楚(实际上我不是 100% 确定):
比较
npm install
(in package directory, no arguments):Install the dependencies in the local node_modules folder.
与
npm install <folder>
:Install a package that is sitting in a folder on the filesystem.
第一个声明安装依赖项,第二个声明安装包。
关于javascript - 为什么当我指示更高版本的 NodeJS 时 "npm install"没有失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22418156/