Introduction to the principle and solution of node. js version management tool n
N is a module of node. It can be used to manage various versions of node. It is similar to Python's rbenv and Ruby's rbenv. The author of n is the famous corner stone.
Install n through npm:
$ npm install -g n
View the current node version:
$ node -vv4.2.4
Use n to install the specified version:
$ n 4.4.4install : 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 node version:
$ Node-vv4.2.4 # same as the original
Solution
If you find that the node version remains unchanged like me, it is most likely that the installation directory of your node is different from the default path of n.
View the current node installation path:
$ Which node/opt/node/bin/node # example
The default installation path of n is/usr/local. If your node is not in this path, n cannot replace bin, lib, include, and share in this path, therefore, we must use the N_PREFIX variable to modify the default node installation path of n.
Edit the environment configuration file:
vim ~/.bash_profile
Insert the following two lines of code to the end of the file:
Export N_PREFIX =/opt/node # actual node installation location export PATH = $ N_PREFIX/bin: $ PATH
: Wq save and exit;
Execute source to make the modification take effect.
$ source ~/.bash_profile
Check whether the environment variables take effect:
echo $N_PREFIX/opt/node
At this time, we need to reinstall:
$ n 4.4.4install : 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:
$ node -vv4.4.4
Indicates that the modification is successful.