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.
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With