*NIX (Linux, OS X, ...)
Use n, an extremely simple Node version manager that can be installed via npm.
Say you want Node.js v0.10.x to build Atom.
npm install -g n # Install n globally
n 0.10.33 # Install and use v0.10.33
Usage:
n # Output versions installed
n latest # Install or activate the latest node release
n stable # Install or activate the latest stable node release
n <version> # Install node <version>
n use <version> [args ...] # Execute node <version> with [args ...]
n bin <version> # Output bin path for <version>
n rm <version ...> # Remove the given version(s)
n --latest # Output the latest node version available
n --stable # Output the latest stable node version available
n ls # Output the versions of node available
Windows
Use nvm-windows, it's like nvm but for Windows. Download and run the installer, then:
nvm install v0.10.33 # Install v0.10.33
nvm use v0.10.33 # Use v0.10.33
Usage:
nvm install [version] # Download and install [version]
nvm uninstall [version] # Uninstall [version]
nvm use [version] # Switch to use [version]
nvm list # List installed versions
Answer from Dennis on Stack Overflow*NIX (Linux, OS X, ...)
Use n, an extremely simple Node version manager that can be installed via npm.
Say you want Node.js v0.10.x to build Atom.
npm install -g n # Install n globally
n 0.10.33 # Install and use v0.10.33
Usage:
n # Output versions installed
n latest # Install or activate the latest node release
n stable # Install or activate the latest stable node release
n <version> # Install node <version>
n use <version> [args ...] # Execute node <version> with [args ...]
n bin <version> # Output bin path for <version>
n rm <version ...> # Remove the given version(s)
n --latest # Output the latest node version available
n --stable # Output the latest stable node version available
n ls # Output the versions of node available
Windows
Use nvm-windows, it's like nvm but for Windows. Download and run the installer, then:
nvm install v0.10.33 # Install v0.10.33
nvm use v0.10.33 # Use v0.10.33
Usage:
nvm install [version] # Download and install [version]
nvm uninstall [version] # Uninstall [version]
nvm use [version] # Switch to use [version]
nvm list # List installed versions
One way is to use NVM, the Node Version Manager.
Use following command to get nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
You can find it at https://github.com/creationix/nvm
It allows you to easily install and manage multiple versions of node. Here's a snippet from the help:
Usage:
nvm install <version> Download and install a <version>
nvm use <version> Modify PATH to use <version>
nvm ls List versions (installed versions are blue)
Videos
nvm install 8.10.0 is for installing proposed node version locally.
In order to use it:
nvm use 8.10.0
Note that you need to run this command as administrator.
You can always set default Node.js version:
nvm alias default 8.10.0
Install
nvm install 8.10.0Use once per terminal
nvm use 8.10.0Set up as the default for all terminals
nvm alias default 8.10.0Set up Node.js version for your IDE
- For more information check nvm documentation
As noted in another answer, there is now a command for this:
nvm now has a command to update npm. It's
nvm install-latest-npmornvm install --latest-npm.
nvm install-latest-npm: Attempt to upgrade to the latest working npm on the current Node.js version.
nvm install --latest-npm: After installing, attempt to upgrade to the latest working npm on the given Node.js version.
Below are previous revisions of the correct answer to this question.
For later versions of npm it is much simpler now. Just update the version that nvm installed, which lives in ~/.nvm/versions/node/[your-version]/lib/node_modules/npm.
I installed Node.js 4.2.2, which comes with npm 2.14.7, but I want to use npm 3. So I did:
cd ~/.nvm/versions/node/v4.2.2/lib
npm install npm
Easy!
And yes, this should work for any module, not just npm, that you want to be "global" for a specific version of node.
In a newer version, npm -g is smart and installs modules into the path above instead of the system global path.
Npm can install itself. Just use npm install with the global flag -g, to overwrite the version of npm currently installed.
Here's an example. (Change 5.4.0 to whichever version you want.)
npm install [email protected] -g
If you switch versions later in nvm, the npm version will change as well, so it's easy to undo this action.
This may work:
nvm install NEW_VERSION --reinstall-packages-from=OLD_VERSION
For example:
nvm install 6.7 --reinstall-packages-from=6.4
then, if you want, you can delete your previous version with:
nvm uninstall OLD_VERSION
Where, in your case, NEW_VERSION = 5.4 OLD_VERSION = 5.0
Alternatively, try:
nvm install node --reinstall-packages-from=current
Or you can update to the last long-term support version with
nvm install lts --reinstall-packages-from=current
You can more simply run one of the following commands:
Latest version:
nvm install node --reinstall-packages-from=node
Stable (LTS) version: (if currently in use)
nvm install "lts/*" --reinstall-packages-from="$(nvm current)"
This will install the appropriate version and reinstall all packages from the currently used node version.
This saves you from manually handling the specific versions.
Kudos to @m4js7er for commenting about the LTS version.
You can set the system-wide Node.js version by typing
nvm alias default 11.10.0
Install the version that you would like:
nvm install 11.10.0
Set 11.10.0 (or another version) as default:
nvm alias default 6.1.0
Verify the current version by running node -v or nvm current
I used nvm for a few years, but the issue I have with it is that it's a per-user installation. When you're dealing with system accounts that have no login (e.g. www-data), you're running into problems and have to jump through hoops to get it working.
An altertive option is asdf, which is installed system-wide: https://asdf-vm.com/guide/getting-started.html. Asdf also provides many plugins for other languages than node.js. That being said, asdf is probably the least stable from all the options.
Another alternative is n: https://github.com/tj/n. This is a pretty stable (and simple, like nvm) node version manager.
Another alternative is Volta: https://docs.volta.sh/guide/getting-started. This one is modern and reliable and should automatically use the right version based on package.json.
Yet another option is fnm: https://github.com/Schniz/fnm
So here you go, I'm sure there's something there for you... Personally, I'm happy with "n". It's simple and fast, all that I need.