Reputation: 224
Is it possible to force my application to run under some specific or upper node version? I found my app may have error if some environment has older node.
I'm wondering if I can set it in package.json. And if there's anything go wrong, it can log a related message to the terminal.
Upvotes: 6
Views: 9116
Reputation: 1500
Node Version Manager (NVM) to Manage Multiple Node.js Versions
Installation
Clone the git repository from github into ~/.nvm or any other folder of your wish.
$ git clone git://github.com/creationix/nvm.git ~/.nvm
To activate it, source it from the bash shell using source or . –
$ . ~/.nvm/nvm.sh
If you want to know what the . does try one of these commands –
$ help source
$ help .
It basically reads and executes all the commands from the filename passed to it (~/.nvm/nvm.sh in our case), in the current shell.
Usage
Using it is super duper simple, let’s see how –
# 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
When you install node v0.10.0, it get’s installed in ~/.nvm/v0.10.0 and the new node binary residing in ~/.nvm/v0.10.0/bin gets added to the PATH environment variable.
$ node -v
v0.10.3
$ nvm install 0.10.0
######################################################################## 100.0%
Now using node v0.10.0
$ node -v
v0.10.0
Upvotes: -1
Reputation: 39512
You can use Node Version Manager for this: https://github.com/creationix/nvm
After installing NVM, you can install as many different versions of Node as you want, and can select which one to run a given app under.
If you want your application to only run with a given version, you can check the version with: process.versions.node
and compare against that. For instance, put this at the beginning of app.js
(or whatever your initial file is):
// 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();
}
(The following is for npm packages only)
After testing different versions, you can specify what versions of Node your package is compatible with in package.json
with the engines
parameter. The following claims to work with all versions equal to or greater than 0.6.0
:
"engines": {
"node": ">=0.6"
}
It should be noted that this does not actually force the user to use >=0.6
, but will give an error if someone tries to npm install
your package. If you want to force a version, you can add "engineStrict": true
.
Upvotes: 3
Reputation: 145994
As @SomeKittens answer describes, you should declare this in your package.json
. However, that doesn't "force" anything. It's just a compatibility statement. If you really want to detect this and take some action such as logging a warning or exiting with an error, you can use process.versions.node
to get the running version, a package like semver
to compute whether you are compatible or not, and take your action.
Generally, this is not something that is commonly done, however. The community is pretty much off 0.6 and older and if you're app can't work on all 0.10 versions, that's kind of lame, and even 0.8 support is usually trivial unless your app is involved with the guts of node somewhere.
From 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.
Upvotes: 0