linux nvm 管理 nodejs,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.linuxidc.com]$ 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

b8bac5ce708ff4de1e739325e5001d0e.png

或:

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

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

安装 Node.js

最新版

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

[linuxidc@localhost www.linuxidc.com]$ 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)

5cf287bd339f7c08256ea9241ca85d34.png

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

[linuxidc@localhost www.linuxidc.com]$ nvm use node

输出如下:

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

62fcead250dd131606940f39fd09bd52.png

稳定版(LTS)

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

[linuxidc@localhost www.linuxidc.com]$ 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)

c5072581ccf62410a0f9e9de4d2e5765.png

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

[linuxidc@localhost www.linuxidc.com]$ 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

e819597037a2012c7239f04819ca2083.png

切换版本

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

1.切换到 v12.11.1,命令:

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

显示:

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

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

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

显示:

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

79d20a86cc8b748a03f5b067b0c01fcf.png

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

使用别名

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

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

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

显示

10 -> v10.16.3

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

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

显示

12 -> v12.11.1

b2ff25d82528fe8b6a707537778953eb.png

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值