Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run application on specific node version

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.

like image 609
George Avatar asked Mar 05 '14 03:03

George


People also ask

How do I use a specific node for a project?

It is a regular npm package that contains only the Node. js binary. To verify, run node -v in your terminal in the root of the project and you should see the version you have set on your machine. Compare that by running npm run v and you should see the version you have set for the project.

How do I specify node version?

The n command for installing and activating a version of Node is simple: n 6.17. 1 . You could also use n latest for the latest version of Node or n lts for the latest LTS version of Node. If the version of Node is already installed, then n will simply switch to that version.

How do I run a specific version of node in Windows?

Wrap up. NVM for Windows allows us to switch between versions of node efficiently. First, we install the node versions we need to work with into NVM using nvm install . We then use nvm use to switch to a specific version of node.

How use NPM specific version?

For npm install specific version, use npm install [package-name]@[version-number]. Use npm view [package-name] version to know the specific latest version of a package available on the npm registry. Use npm list [package-name] to know the specific latest version of an installed package.


1 Answers

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.

like image 74
SomeKittens Avatar answered Sep 23 '22 05:09

SomeKittens