Switch between multiple Node.js versions

This article will talk about switching between multiple node.js versions under windows and Mac systems.

1. Use nvm (nvm-window) under windows

2. Use nvm and n under Mac

Note: The nvm addresses under windows and mac are different

1. Windows system

What is nvm?

nvm is a simple bash script used to manage multiple existing Node.js versions on the system

configuration steps

1. First uninstall the original node version in this system (you can also not uninstall it, I think it is better to uninstall it, so as to avoid conflicts. Anyway, I uninstalled it);

I won't go into details about uninstalling, it can be uninstalled in the programs and features of the control panel.

2. Go to github to download the nvm installation package

Download address: https://github.com/coreybutler/nvm-windows/releases

download instructions

After that, it is installed, unzip and double-click the exe file to install it foolishly.

It should be noted that the best installation path is C:\nvm. After the default path is successfully installed, there will be problems when switching node versions.

In addition, after the installation is successful, the system variables will be automatically configured. Don't worry about it here, just know that there is such a thing, so as not to misuse it when modifying environment variables in the future.

Take a screenshot:

variable

NVM_HOME: the installation directory of nvm

NVM_SYMLINK: node installation directory

Environment variables:

By the way, after downloading, find the installation directory of nvm, open the setting.txt file, and add the following two configurations:

nvm node_mirror https://npm.taobao.org/mirrors/node/

nvm npm_mirror https://npm.taobao.org/mirrors/npm/

Take a screenshot

setting configuration

This step can be done or not. The purpose of the configuration is to switch the npm mirror to the Taobao mirror, which can improve the download speed. (I just like this kind of pleasure)

Next, verify that the installation was successful

Enter in cmd: nvm The following picture appears and it is successful

Successful screenshot

3. Install node

To install node, you must first know which node versions are available, otherwise you will definitely report an error (this is nonsense)

You can see it here: https://github.com/coreybutler/nodedistro/blob/master/nodeversions.json

It is not recommended to watch it here, reason 1. It is slow to open the github website. 2. The above version is not the latest

You can also directly use the command to view: nvm list available

node version list

Then use the nvm command to download nodejs

(1) I take version 10.13.0 as an example

npm install 10.13.0

(2) Use the nvm use 10.13.0 command to switch the node version to 10.13.0

nvm use 10.13.0

Note: This is very important. Use the nuv use command immediately after installation, otherwise you will not be able to use this node version. (This is a pitfall, MD, it wasted my whole morning time, 10,000 words are omitted here, and I answered a word that the dumplings should be eaten hot)

If you really can't switch this version, use nvm uninstall 10.13.0 to uninstall this version and reinstall it.

(3) Use nvu list to view all downloaded node versions, and which version is currently (currently version 10.13.0, I downloaded the two versions 9.3.0 and 12.13.1 by the way)

nvu list

(4) Run the node command to see if it is successful

Check node version

This is enough, so far nvm installation is successful

Some commonly used nvm commands

nvm install <version> [arch]: This can be the node.js version or the latest stable version latest. (Optional [arch]) Specifies to install the 32-bit or 64-bit version (defaults to system arch). Set [arch] to all to install both 32 and 64 bit versions.

nvm list [available]: List the installed node.js versions. Optional available, displays a partial list of downloadable versions. This command can be abbreviated as nvm ls [available].

nvm uninstall <version>: Uninstall the specified version of nodejs.

nvm use [version] [arch]: Switch to using the specified nodejs version. 32/64 bit [arch] can be specified.

There are some other commands, so I won’t give examples one by one.

2. Mac system

The mac system installs one of nvm and n.

Let's start with n

The following are three installation methods (choose one of them)

(1) If you already have node on your mac, you can install it directly using the npm command

npm install -g n

After installation, the version n is cached in N_PREFIX in the node subdirectory n/versions of the directory specified by the environment variable, and the default is /usr/local; while the active  node version is directly installed in N_PREFIX.

To avoid the need for sudo n and npm global installs, it is recommended that you either install into your home directory using N_PREFIX , or take ownership of the system directory:

# make cache folder (if missing) and take ownership

sudo mkdir -p /usr/local/nsudo chown -R $(whoami) /usr/local/n

# take ownership of node install destination folders

sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share

(2) If there is no node, you can also install it in another way

curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -on

bash n lts

# Now node and npm are available

(3) Use third-party installation

curl -L https://git.io/n-install | bash

Because I already have node, here I installed it through the first method.

1. First execute npm install -gn

install n

2. Check whether the installation is successful n --version

View version of n

3. If you run n directly, you will be prompted that there is no node version that can be switched. Although node has been installed before, you still need to install it before you can switch

Check the installed node version, the figure shows that no node is installed

4. Install node version, n 10.13.0

Install the node version

If you encounter Error: sudo required during installation

At this time, use sudo n 10.13.0

Install node with sudo

The command format is: n node-version

Such as: n 10.13.0 install version 10.13.0

      n latest installs the latest official release

     n stable installs the stable official release

     n lts installs the latest official LTS version

My Mac system, all installed node version storage path: /usr/local/n/versions/node

5. Switch node version

Execute n first 

View the installed node version, the selected one is the current node version

Press the up and down keys on the keyboard to select the version, then press Enter to switch the version

At this time, the following error will appear, and there is no permission. There are three solutions:

No permission

(1) Use sudo n directly, then select the node version, and press Enter.

 sudo n

(2) sudo su Switch to the root user, then use n to view the version, then select the node version, and press Enter.

sudo su

(3) Use sudo n 10.13.0 Yes, this is the installation command, if this version is not installed, then install this version, if this version is already installed, then switch this node version.

sudo n

After switching, take a look at this node version node -v

node -

Perfect, the installation is complete.

By the way, if you want to uninstall a certain node version, you can just n rm 10.13.0 (add sudo if you don’t have permission)

Finally, some students have the situation that switching is not easy, I have not encountered it, but then I also try to read some posts here, recommend one: https://blog.csdn.net/ruffaim/article/details/83578710

If I encounter such problems in the future, I will update the article in time

Let’s talk about using nvm under Mac

Generally speaking, it is not much different from using nvm under windows

To install nvm, it is recommended to uninstall the original node first, don’t ask me why, I don’t know, there is a high probability that something will go wrong

The following is the uninstall node command 

npm ls -g --depth=0 # View the modules that have been installed globally, so as to delete these global modules and then reinstall them globally according to different node versions

sudo rm -rf /usr/local/lib/node_modules # Delete the global node_modules directory

sudo rm /usr/local/bin/node # remove node

cd /usr/local/bin && ls -l | grep "../lib/node_modules/" | awk '{print $9}'| xargs rm # Delete the soft link registered by the global node module

After that, it is a little different to install nvm. Enter the following command directly on the command line

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash

Then enter nvm, when you see the output, it means that the installation is successful, if it says that nvm did not find command not found, it means that the installation failed

Add the following code to the .bash_profile file to configure environment variables.

export NVM_DIR="$HOME/.nvm"

[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm

Save the file directly after editing.

Then enter "source .bash_profile" in the terminal to update the configured environment variables.

If you don't have this file, you can create one. Reference study articles: http://blog.sina.com.cn/s/blog_8444778b0102x68e.html

At this point, nvm is installed. For the specific usage method, please refer to the nvm operation under the window. For details, please refer to the window part.

Some commonly used nvm commands

nvm install stable //Install the latest version 

nodenvm install [node version number] //Install the specified version

nodenvm use [node version number] //Switch to the specified version of node

nvm alias default [node version number] //Set the default version

Guess you like

Origin blog.csdn.net/weixin_43167662/article/details/130747614