【发布时间】:2014-04-06 22:33:31
【问题描述】:
是否可以强制我的应用程序在某些特定或更高节点版本下运行? 我发现如果某些环境的节点较旧,我的应用程序可能会出错。
我想知道是否可以在 package.json 中设置它。如果出现任何问题,它可以将相关消息记录到终端。
【问题讨论】:
标签: javascript node.js web
是否可以强制我的应用程序在某些特定或更高节点版本下运行? 我发现如果某些环境的节点较旧,我的应用程序可能会出错。
我想知道是否可以在 package.json 中设置它。如果出现任何问题,它可以将相关消息记录到终端。
【问题讨论】:
标签: javascript node.js web
您可以为此使用节点版本管理器:https://github.com/creationix/nvm
安装 NVM 后,您可以安装任意多个不同版本的 Node,并且可以选择在哪个版本下运行给定的应用程序。
如果您希望您的应用程序仅以给定版本运行,您可以使用以下代码检查版本:process.versions.node 并与之进行比较。例如,将其放在 app.js 的开头(或任何您的初始文件):
// Using package semver for clarity
var semver = require('semver');
if (!semver.satisfies(process.versions.node, '>0.11.0')) {
console.log('Incorrect Node version');
process.exit();
}
(以下仅适用于 npm 包)
测试不同版本后,您可以在package.json 中使用engines 参数指定您的包兼容的Node 版本。以下声明适用于等于或大于 0.6.0 的所有版本:
"engines": {
"node": ">=0.6"
}
需要注意的是,这实际上并不会强制用户使用 >=0.6,但如果有人试图npm install 你的包会报错。如果要强制一个版本,可以加"engineStrict": true。
【讨论】:
正如@SomeKittens 的回答所描述的,您应该在您的package.json 中声明这一点。但是,这并没有“强迫”任何事情。这只是一个兼容性声明。如果您真的想检测到这一点并采取一些措施,例如记录警告或退出错误,您可以使用process.versions.node 获取正在运行的版本,使用semver 之类的包来计算您是否兼容,并且采取行动。
但是,一般来说,这不是常见的事情。社区几乎没有 0.6 和更早的版本,如果您的应用程序无法在所有 0.10 版本上运行,那就有点蹩脚了,甚至 0.8 支持通常也是微不足道的,除非您的应用程序在某处涉及节点的胆量。
来自npm help json:
engines
You can specify the version of node that your stuff works on:
{ "engines" : { "node" : ">=0.10.3 <0.12" } }
And, like with dependencies, if you don't specify the version (or if you specify "*" as the ver-
sion), then any version of node will do.
If you specify an "engines" field, then npm will require that "node" be somewhere on that list.
If "engines" is omitted, then npm will just assume that it works on node.
You can also use the "engines" field to specify which versions of npm are capable of properly
installing your program. For example:
{ "engines" : { "npm" : "~1.0.20" } }
Note that, unless the user has set the engine-strict config flag, this field is advisory only.
engineStrict
If you are sure that your module will definitely not run properly on versions of Node/npm other
than those specified in the engines hash, then you can set "engineStrict": true in your pack-
age.json file. This will override the user's engine-strict config setting.
Please do not do this unless you are really very very sure. If your engines hash is something
overly restrictive, you can quite easily and inadvertently lock yourself into obscurity and pre-
vent your users from updating to new versions of Node. Consider this choice carefully. If peo-
ple abuse it, it will be removed in a future version of npm.
【讨论】:
用于管理多个 Node.js 版本的节点版本管理器 (NVM)
安装
将 git 存储库从 github 克隆到 ~/.nvm 或您希望的任何其他文件夹。
$ git clone git://github.com/creationix/nvm.git ~/.nvm
要激活它,请使用 source 或 . ——
$ . ~/.nvm/nvm.sh
如果你想知道 .确实尝试了这些命令之一——
$ help source
$ help .
它基本上在当前 shell 中从传递给它的文件名(在我们的例子中为 ~/.nvm/nvm.sh)读取并执行所有命令。
用法
使用起来超级简单,让我们看看如何——
# To check what versions can be installed
$ nvm ls-remote
# To install:
# nvm install [version]
$ nvm install 0.10.0
# To check what versions are installed
$ nvm ls
# To use the installed version
$ nvm use 0.10.0
# .. or just run the node repl with that version
$ nvm run 0.10.0
# To remove/uninstall
$ nvm uninstall 0.10.0
当您安装节点 v0.10.0 时,它会安装在 ~/.nvm/v0.10.0 中,并且驻留在 ~/.nvm/v0.10.0/bin 中的新节点二进制文件会添加到 PATH 环境变量中。
$ node -v
v0.10.3
$ nvm install 0.10.0
######################################################################## 100.0%
Now using node v0.10.0
$ node -v
v0.10.0
【讨论】: