CentOS 7.6下安装 NVM 管理不同版本的 Node.js


nvm全称Node Version Manager是 Nodejs 版本管理器,它让我们能方便的对 Node.js 的版本进行切换。 nvm 的官方版本只支持 Linux 和 Mac。 Windows 用户,可以用 nvm-windows。详情请点击官方说明。

安装NVM

查资料得出,要使用 curl 或 wget 来安装nvm(版本可以选用官网最新版):

[linuxidc@localhost www.linuxboy.net]$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh | bash

输出示例如下:

  % Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
                                Dload  Upload  Total  Spent    Left  Speed
100 13527  100 13527    0    0  22484      0 --:--:-- --:--:-- --:--:-- 22545
=> Downloading nvm from git to '/home/linuxidc/.nvm'
=> 正克隆到 '/home/linuxidc/.nvm'...
remote: Enumerating objects: 286, done.
remote: Counting objects: 100% (286/286), done.
remote: Compressing objects: 100% (256/256), done.
remote: Total 286 (delta 34), reused 93 (delta 17), pack-reused 0
接收对象中: 100% (286/286), 146.90 KiB | 0 bytes/s, done.
处理 delta 中: 100% (34/34), done.
=> Compressing and cleaning up git repository

=> Appending nvm source string to /home/linuxidc/.bashrc
=> Appending bash_completion source string to /home/linuxidc/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

CentOS 7.6下安装 NVM 管理不同版本的 Node.js

或:

[linuxidc@localhost www.linuxboy.net]$ wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh | bash

注意:安装完了,重新打开终端 Terminal来重启会话

安装 Node.js

最新版

1、安装最新版 Node.js 命令:

[linuxidc@localhost www.linuxboy.net]$ nvm install node
Downloading and installing node v12.11.1...
Downloading https://nodejs.org/dist/v12.11.1/node-v12.11.1-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.11.1 (npm v6.11.3)
Creating default alias: default -> node (-> v12.11.1)

CentOS 7.6下安装 NVM 管理不同版本的 Node.js

2.查看安装效果,命令:

[linuxidc@localhost www.linuxboy.net]$ nvm use node

输出如下:

Now using node v12.11.1 (npm v6.11.3)

CentOS 7.6下安装 NVM 管理不同版本的 Node.js

稳定版(LTS)

1、安装 LTS 版,命令:nvm install --lts

[linuxidc@localhost www.linuxboy.net]$ nvm install --lts
Installing latest LTS version.
Downloading and installing node v10.16.3...
Downloading https://nodejs.org/dist/v10.16.3/node-v10.16.3-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v10.16.3 (npm v6.9.0)

CentOS 7.6下安装 NVM 管理不同版本的 Node.js

2、查看安装效果,命令:
[linuxidc@localhost www.linuxboy.net]$ nvm list

输出如下:

      v10.16.3
->    v12.11.1
default -> node (-> v12.11.1)
node -> stable (-> v12.11.1) (default)
stable -> 12.11 (-> v12.11.1) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/dubnium (-> v10.16.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.1 (-> N/A)
lts/dubnium -> v10.16.3

CentOS 7.6下安装 NVM 管理不同版本的 Node.js

切换版本

从上面的安装列表上已经可以看到,我们安装了一个最新版,一个稳定版。分别是:v12.11.1 和 v10.16.3,我们要如何切换不同版本呢?
1.切换到 v12.11.1,命令:

[linuxidc@localhost www.linuxboy.net]$ nvm use v12.11.1

显示:

Now using node v12.11.1 (npm v6.11.3)

2.切换到 v10.16.3,命令:

[linuxidc@localhost www.linuxboy.net]$ nvm use v10.16.3

显示:

Now using node v10.16.3 (npm v6.9.0)

CentOS 7.6下安装 NVM 管理不同版本的 Node.js

到这里,我们基本会使用 nvm 了,想用什么版本就可以自由切换。 但如果想玩得更爽一点,就要学习如下一些技巧。

使用别名

你肯定也想到,每次输入v10.16.3 好麻烦。并且时间长了,不一定记得住后面是16.3,还是18.3的版本号。

1、设定 LTS 版别名,命令:

[linuxidc@localhost www.linuxboy.net]$ nvm alias 10 v10.16.3

显示

10 -> v10.16.3

2、设定最新版别名,命令:

[linuxidc@localhost www.linuxboy.net]$ nvm alias 12 v12.11.1

显示

12 -> v12.11.1

CentOS 7.6下安装 NVM 管理不同版本的 Node.js

linuxboy的RSS地址:https://www.linuxboy.net/rssFeed.aspx

本文永久更新链接地址:https://www.linuxboy.net/Linux/2019-10/160918.htm

相关内容