前言
NVM是node.js的版本管理器。他可以实现多版本共存,以及版本切换。在终端使用起来很方便。本文介绍在debain上以脚本的方式安装,希望对你有帮助
实验环境
OS: Debian 11
安装
安装或更新nvm,可以使用下面这个脚本,任选其一
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
演示
root@xiaoran:~# wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
=> Downloading nvm from git to '/root/.nvm'
=> Cloning into '/root/.nvm'...
remote: Enumerating objects: 355, done.
remote: Counting objects: 100% (355/355), done.
remote: Compressing objects: 100% (302/302), done.
remote: Total 355 (delta 39), reused 168 (delta 28), pack-reused 0
Receiving objects: 100% (355/355), 208.50 KiB | 2.90 MiB/s, done.
Resolving deltas: 100% (39/39), done.
* (HEAD detached at FETCH_HEAD)
master
=> Compressing and cleaning up git repository
=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.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
到这里后,关闭终端再打开,或者运行以下命令激活NVM,推荐前者
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
使用nvm -v
查看安装的版本,显示则安装成功
root@xiaoran:~# nvm -v
0.39.1
安装node
root@xiaoran:~# nvm install node
Downloading and installing node v18.8.0...
Downloading https://nodejs.org/dist/v18.8.0/node-v18.8.0-linux-x64.tar.xz...
################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v18.8.0 (npm v8.18.0)
Creating default alias: default -> node (-> v18.8.0)
root@xiaoran:~# npm -v
8.18.0
root@xiaoran:~# node -v
v18.8.0
常用命令
nvm install node # 默认安装的最新版本
nvm ls-remote # 查看可以使用的版本
nvm install 14.7.0 #安装指定版本
nvm use <版本号> #切换指定版本eg: nvm use v14.7.0
nvm list # 查看本地已经安装的版本
演示
root@xiaoran:~# nvm install 14.7.0 #按照14.7.0版本
Downloading and installing node v14.7.0...
Downloading https://nodejs.org/dist/v14.7.0/node-v14.7.0-linux-x64.tar.xz...
################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v14.7.0 (npm v6.14.7)
root@xiaoran:~# node -v
v14.7.0
root@xiaoran:~# nvm use node
Now using node v18.8.0 (npm v8.18.0)
root@xiaoran:~# nvm use 14.7.0
Now using node v14.7.0 (npm v6.14.7)
root@xiaoran:~# nvm use node
Now using node v18.8.0 (npm v8.18.0)
root@xiaoran:~# node -v
v18.8.0
root@xiaoran:~# nvm list
v14.7.0
-> v18.8.0
root@xiaoran:~# nvm use v14.7.0
Now using node v14.7.0 (npm v6.14.7)
root@xiaoran:~# nvm list
-> v14.7.0
v18.8.0
root@xiaoran:~#
小结
对nvm的使用,也仅仅是安装切换使用阶段,目前来说也够用了。nvm可以实现不同版本切换node,确实听不错的。
评论区