在特定节点版本上运行应用程序

是否有可能强制我的应用程序运行在某些特定的或较高的节点版本? 我发现我的应用程序可能有错误,如果某些环境有较旧的节点。

我想知道是否可以在package.json中设置它。 如果出现任何问题,可以将相关消息logging到terminal。

您可以使用节点版本pipe理器: https : //github.com/creationix/nvm

安装NVM之后,您可以根据需要安装尽可能多的不同版本的节点,并可以select在哪个节点下运行给定的应用程序。

如果你希望你的应用程序只能运行给定的版本,你可以用: 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软件包)

在testing了不同的版本之后,你可以使用engines参数指定你的软件包与package.json兼容的版本。 以下声明适用于等于或大于0.6.0所有版本:

  "engines": { "node": ">=0.6" } 

应该指出的是,这实际上并没有强制用户使用 >=0.6 ,但是如果有人试图用npm install你的软件包,将会出错。 如果你想强制一个版本,你可以添加"engineStrict": true

正如@SomeKittens答案所描述的,你应该在你的package.json声明这个。 但是,这并不“强迫”任何东西。 这只是一个兼容性声明。 如果你真的想检测这个,并采取一些行动,如logging一个警告或退出一个错误,你可以使用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. 

节点版本pipe理器(NVM)pipe理多个Node.js版本

安装

从github克隆git仓库到〜/ .nvm或你想要的任何其他文件夹。

 $ git clone git://github.com/creationix/nvm.git ~/.nvm 

要激活它,使用源或bash shell来源。 –

 $ . ~/.nvm/nvm.sh 

如果你想知道什么。 确实尝试这些命令之一 –

 $ help source $ help . 

它基本上读取和执行所有的命令(在我们的例子中是〜/ .nvm / nvm.sh),在当前的shell中。

用法

使用它是超级麻烦简单,让我们看看如何 –

 # 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环境variables中。

 $ node -v v0.10.3 $ nvm install 0.10.0 ######################################################################## 100.0% Now using node v0.10.0 $ node -v v0.10.0