Introduction
n is a module of node, which can be used to manage various versions of node. Similar to pyenv in Python and rbenv in Ruby. The author of n is the famous TJ master.
Install n via npm:
$ npm install -g n
View the current node version:
$ node -v v4.2.4
Install the specified version via n:
$ n 4.4.4 install : node-v4.4.4 mkdir : /opt/node/n/versions/node/4.4.4 fetch : https://nodejs.org/dist/v4.4.4/node-v4.4.4-linux-x64.tar.gz ###################################100.0% installed : v4.2.4
Check the current version again node version:
$ node -v v4.2.4 #和原来一样
Solution
If you are like me and find that there is no change in the node version, then the most likely situation is that your node installation directory is different from the default path.
Check the current installation path of node:
$ which node /opt/node/bin/node #举个例子
The default installation path is /usr/local. If your node is not in this path, you cannot copy bin, lib, include, and share when switching versions. in this path, so we must modify the default node installation path of n through the N_PREFIX variable.
Edit the environment configuration file:
vim ~/.bash_profile
Insert the following two lines of code at the end of the file:
export N_PREFIX=/opt/node #node实际安装位置 export PATH=$N_PREFIX/bin:$PATH
:wq save and exit;
Execute source to make the modifications effective.
$ source ~/.bash_profile
Confirm whether the environment variable is effective:
echo $N_PREFIX /opt/node
At this time we need to reinstall:
$ n 4.4.4 install : node-v4.4.4 mkdir : /opt/node/n/versions/node/4.4.4 fetch : https://nodejs.org/dist/v4.4.4/node-v4.4.4-linux-x64.tar.gz ##############100.0% installed : v4.4.4
Check the current node version again:
$ node -v v4.4.4
Description modification success.