This situation is often encountered in our daily development: there are several projects on hand, each project has different requirements, and different projects must rely on different versions of the NodeJS runtime environment. This problem can be tricky without the right tools.
NVM came into being. NVM is a node management tool for Mac. It is similar to Ruby's rvm. If you need to manage nodes under Windows, it is officially recommended to use nvmw or nvm-windows. However, nvm-windows is not a simple port of nvm, and they have nothing to do with it. But all the commands described below can be run in nvm-windows.
Difference between nvm and n
The node version management tool also has the n command of the TJ god, the n command exists as a node module, and nvm is an external shell script independent of node / npm, so the n command is more limited than nvm.
Because the module paths installed by npm are all / usr / local / lib / node_modules, when n is used to switch between different node versions, the global node / npm directory is actually shared. Therefore, it cannot satisfy the requirement of "using different global node modules according to different node versions".
Uninstall globally installed node / npm
The node installation package downloaded from the official website will be automatically installed in the global directory after running. It often encounters some permission problems during use, so it is recommended to uninstall the globally installed node / npm according to the following method.
First, open your Finder, press shift + command + G, open the window to the folder, enter the following directories respectively and delete the files and folders related to node and node_modules:
Open / usr / local / lib and delete the files and folders related to node and node_modules
Open / usr / local / include and delete the files and folders related to node and node_modules
If you installed NodeJS using brew install node, then you also need to execute the brew uninstall node command in the terminal to uninstall
Check all local, lib, and include folders under your personal home folder, and delete all files and folders related to node and node_modules
Open / usr / local / bin and delete the node executable
You may also need to enter some additional instructions 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
First and foremost: Be sure to uninstall the installed NodeJS, otherwise conflicts will occur. Then download the latest nvm-windows installation package and install it directly.
OS X / Linux installation
Unlike Windows, we don't have to uninstall the original NodeJS first. Of course, we recommend that you uninstall it first. In addition, you also need a C ++ compiler. Linux distributions generally do not have to worry about it. For Ubuntu, you can directly use the build-essential package. For OS X, you can use the X-Code command line tool. Just run this command: xcode-select --install
On 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
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
Download the install.sh script from a remote and execute it. Note that this version of the year number v0.33.0 will change as the project develops. It is good to check the latest installed version at any time through the official latest installation command.
Install multiple versions of node / npm
For example, if we want to install the 4.2.2 version, we can use the following command: nvm install 4.2.2
nvm follows the semantic versioning rules. For example, if you want to install the latest version of the latest 4.2 series, you can run:
nvm install 4.2
nvm will look for the highest version of 4.2.x to install.
You can use the following command to list all available versions on the remote server:
nvm ls available
Switch between different versions
Whenever we install a new version of Node, the global environment will automatically set this new version as the default.
nvm provides the nvm use command. This command is used similarly to the install command.
For example, switch to 4.2.2:
nvm use 4.2.2
Switch to the latest 4.2.x:
nvm use 4.2 switch to iojs:
nvm use iojs-v3.2.0 switches to the latest version: each time nvm use node performs a switch, the system will link the node's executable file to a specific version of the file.
We can also alias different version numbers with nvm:
nvm alias awesome-version 4.2.2 We gave 4.2.2 a version number called awesome-version, then we can run: nvm use awesome-version The following command can cancel the alias: nvm unalias awesome-version
In addition, you can also set the special alias of default:
nvm alias default node
List installed instances
The green arrow above is the version currently in use, and the aliases that have been set are listed below.
Use different versions of Node in your project
We can specify the Node version to use by creating a .nvmrc file in the project directory. Then execute nvm use in the project directory. The contents of the .nvmrc file need only follow the semantic versioning rules mentioned above. There is another tool called avn that can automate this process.
How does npm work in multiple environments? Each version of Node comes with a different version of npm. You can use npm -v to view the npm version. Globally installed npm packages are not shared across different Node environments as this can cause compatibility issues. They are placed in directories of different versions, such as ~ / .nvm / versions / node / / lib / node_modules . This also saves us the effort of using sudo in Linux. Because this is the user's home folder, it does not cause permissions issues.
But here comes the problem. We have to reinstall the npm package we installed? Fortunately, we have a way to solve our problem. Run the following command to import from a specific version to the new version of Node that we will install:
nvm install v5.0.0 --reinstall-packages-from = 4.2
Other commands directly run a specific version of Nodenvm run 4.2.2 --version run a specific version of Node in a child process of the current terminal
nvm exec 4.2.2 node --version confirm the path of a certain version of Node
nvm which 4.2.2 installs other Node implementations, such as iojs (an ES6-based Node implementation, which has now been merged with Node)
nvm install iojs-v3.2.0 shortcut command:
nvm install node
nvm install iojs install the latest version of iojs
nvm install unstable install the latest unstable version of Node