SoFunction
Updated on 2025-03-03

The principle and solution to the invalid version management tool n

Introduction

n is a module of node that can be used to manage various versions of node. Similar to pyenv in Python and rbenv in Ruby. n's author 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 : /dist/v4.4.4/node-v4.4.
###################################100.0%
  installed : v4.2.4

Check the current node version again:

$ node -v
v4.2.4 #Same as before

Solution

If you are like me and find that the node version has not changed, the most likely situation is that the installation directory of your node is different from the default path of n.

Check the node's current installation path:

$ which node
/opt/node/bin/node #Give an example

The default installation path of n is /usr/local. If your node is not under this path, the n switch version cannot copy bin, lib, include, and share in this path, so 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 at the end of the file:

export N_PREFIX=/opt/node #node actual installation locationexport PATH=$N_PREFIX/bin:$PATH

:wqSave and exit;

Execute source to make the modification take effect.

$ 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 : /dist/v4.4.4/node-v4.4.
##############100.0%
  installed : v4.4.4

Check the current node version again:

$ node -v
v4.4.4

It means the modification is successful.

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.