Home  >  Article  >  Web Front-end  >  How to use nvm to install and manage multiple versions of node.js on Mac and Windows?

How to use nvm to install and manage multiple versions of node.js on Mac and Windows?

青灯夜游
青灯夜游Original
2020-09-09 10:21:263292browse

This article will show you how to use nvm to install and manage multiple versions of node.js on Mac and Windows. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to use nvm to install and manage multiple versions of node.js on Mac and Windows?

What I wrote before

There are currently two main ways to install Node.js. [Video tutorial recommendation: node js tutorial]

1. Get the latest version from the official website

Install Node.js The easiest way is to obtain the latest version of the installation file from the official website.

It is worth noting:

  • #In this way, every time you update the version of Node.js, you need to download it from the official website, and Perform an overlay installation.

  • In this way, only one version of Node.js can exist in the system, which is not suitable for comparative study.

2. Use nvm version manager

nvm is developed by Tim Caswell This software is used in Mac systems to manage multiple versions of Node.js through commands.

nvm software related introduction, you can visit The project's github homepage.

If it is a Windows system, you can refer to the github homepage of nvm-windows.

Install Node.js through nvm

1. Install nvm version manager

Open the "Terminal" window and enter the following command to install nvm software online:

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

It is worth noting:

If you want to successfully install nvm , Xcode software must be installed first under Mac system.

Since the Xcode software is about 4.3GB, if you don’t want to install the Xcode software and want to successfully install nvm, you can do it as follows.

  • Open the "Terminal" window and enter the following command.

xcode-select --install
  • After executing the above command, the software installation prompt window will automatically pop up. Click the [Install] button to install. (This software is about 130MB)

The above steps can replace the installation of Xcode software to ensure the successful installation of nvm software.

If it is a Windows system, you can download the installation file from https://github.com/coreybutler/nvm-windows/releases and install it.

After successful installation, in the "Terminal" window, enter the nvm command to verify whether nvm is successfully installed.

2. Use nvm to install Node.js

The following operations are completed in the "Terminal" window.

1) Install the specified version of Node.js

We can install the specified version of Node.js online through the following nvm command.

nvm install [nodeversion]

For example, if you need to install the v6.9.1 version of Node.js, you can complete it with the following command.

nvm install v6.9.1

2) Specify the currently used Node.js version

Multiple versions of Node.js can be installed at the same time through nvm, and we can specify the use of a certain version .

nvm use [nodeversion]

For example, if you need to use the v6.9.1 version of Node.js, you can use the following command.

nvm use v6.9.1

3) Check the list of currently installed Node.js versions

Since multiple Node.js can be installed through nvm, it is difficult to manage too many versions. We can also check which versions of Node.js are currently installed at any time.

nvm ls

4) Other commands of nvm

nvm also provides some commands to facilitate our daily management of Node.js versions.

  • nvm uninstall [nodeversion]: Indicates to delete the specified version of Node.js. The usage is similar to the install command.

  • nvm current: Indicates the currently used Node.js version.

  • nvm reinstall-packages [npmversion]: Indicates that under the current Node.js version, install the specified version of the npm package manager.

3. The significance of installing multiple versions of Node.js

Since the establishment of the Node.js Foundation, Node .js has a release plan, which means there are two releases at the same time: a stable version and a trial version.

In Node.js, stable versions with long-term support (LTS) start with even numbers (4,6,8...), while experimental versions start with odd numbers (5, 7.. .). We recommend using LTS versions in production environments and using experimental versions to try new things.

4. Domestic environmental issues

由于国内在一些情况下有些特殊。Node.js 官方镜像源又在国外,经常通过 nvm 安装 Node.js 时,速度比较慢,或者没有响应。

根据这种情况,nvm 允许更改安装的镜像源,我们可以将镜像源切换到国内的淘宝提供的镜像源。

1) Mac 系统的 nvm 软件切换镜像源

根据 nvm 官方提供的帮助文档,我们可以通过以下命令进行切换。

export NVM_NODEJS_ORG_MIRROR="http://npm.taobao.org/mirrors/node"

http://npm.taobao.org/mirrors/node淘宝NPM镜像 提供的国内 Node.js 的安装镜像源。

值得注意的是:

种方式,在每次重启“终端”会失效。也就是说,每次打开“终端”都需要执行上述命令。

如果并不想每次打开“终端”,都需要重新设置 NVM_NODEJS_ORG_MIRROR 环境变量。

  • 如果“终端”使用的是 bash Shell 的话(一般是 Mac 系统终端默认)向 ~/.bash_profile 文件(如果没有,会自动创建)增加以下内容:

# nvm
export NVM_NODEJS_ORG_MIRROR="http://npm.taobao.org/mirrors/node"
source ~/.nvm/nvm.sh
  • 如果“终端”使用的是 zsh Shell 的话(一般是 Mac 开发人员使用)向 ~/.zshrc 文件(如果没有,会自动创建)增加以下内容:

# nvm
export NVM_NODEJS_ORG_MIRROR="http://npm.taobao.org/mirrors/node"
source ~/.nvm/nvm.sh

2) Windows 系统的 nvm-windows 软件切换镜像源

我们可以找到 nvm-windows 软件的安装目录中的 settings.txt 文件,增加以下内容:

node_mirror=http://npm.taobao.org/mirrors/node/

添加成功之后,需要重新打开命令行窗口。

更多编程相关知识,可访问:编程入门!!

The above is the detailed content of How to use nvm to install and manage multiple versions of node.js on Mac and Windows?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn