Managing Different Versions of Node and npm with nvm
Category Programming Techniques
In our daily development, we often encounter this situation: we have several projects on hand, each with different requirements, which means different projects must rely on different versions of the NodeJS runtime environment. Without a suitable tool, this problem will be very difficult to handle.
nvm was born for this purpose. nvm is a Node management tool for Mac, similar to rvm for managing Ruby. If you need to manage Node on Windows, the official recommendation is to use nvmw or nvm-windows. However, nvm-windows is not a simple port of nvm, and they have no relationship. But all the commands introduced below can be run in nvm-windows.
The Difference Between nvm and n
Another Node version management tool is TJ's n command. The n command exists as a Node module, while nvm is an external shell script independent of Node/npm. Therefore, the n command is more limited compared to nvm.
Since the paths of npm-installed modules are all /usr/local/lib/node_modules
, when using n to switch between different Node versions, the global Node/npm directory is actually shared. Therefore, it cannot well meet the need to "use different global Node modules according to different Node versions."
Uninstall Globally Installed Node/npm
The Node installation package downloaded from the official website will automatically install in the global directory, and you will often encounter some permission issues during use. Therefore, it is recommended to uninstall the globally installed Node/npm as follows.
First, open your Finder, press shift+command+G
, open the go-to-folder window, and enter the following directories to delete the node
and node_modules
related files and folders:
Open
/usr/local/lib
and delete thenode
andnode_modules
related files and folders.Open
/usr/local/include
and delete thenode
andnode_modules
related files and folders.If you installed NodeJS with
brew install node
, you also need to execute thebrew uninstall node
command in the terminal to uninstall.Check all the
local
,lib
, andinclude
folders in your personal home folder, and delete all files and folders related tonode
andnode_modules
.Open
/usr/local/bin
and delete thenode
executable file.
You may also need to enter some additional commands in your terminal:
sudo rm /usr/local/bin/npm
sudo rm /usr/local/share/man/man1/node.1
sudo rm /usr/local/lib/dtrace/node.d
sudo rm -rf ~/.npm
sudo rm -rf ~/.node-gyp
sudo rm /opt/local/bin/node
sudo rm /opt/local/include/node
sudo rm -rf /opt/local/lib/node_modules
Windows Installation
The most important thing first: be sure to uninstall the installed NodeJS to avoid conflicts. Then download the latest installation package of nvm-windows and install it directly.
OS X/Linux Installation
Unlike Windows, we don't necessarily have to uninstall the original NodeJS. Of course, it is still recommended to uninstall it first. In addition, you also need a C++ compiler. Linux distributions generally don't have to worry, like Ubuntu can directly use the build-essential
suite, for OS X, you can use the command-line tools of X-Code. Run this command to do so:
xcode-select --install
In Linux: (If it is a Debian distribution)
sudo apt-get install build-essential
Then we can use
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
or
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
to download the install.sh
script from the remote and execute it. Note that this version number v0.33.0
will change with the development of the project. It is beneficial to check the latest installation version at any time through the official latest installation command.
Installing Multiple Versions of Node/npm
For example, if we want to install version 4.2.2, we can use the following command:
```