还在为管理node版本发愁吗?快来用用nvm 吧!

什么是 nvm 为什么要使用nvm

NVM(Node Version Manager)是一个用于管理 Node.js 版本的工具。它允许在同一台计算机上安装和切换多个 Node.js 版本。可以使用 NVM 来测试不同版本的 Node.js,或在不同的项目中使用不同的 Node.js 版本。

NVM 的主要价值在于它可以让开发者 轻松地 管理 和 切换 多个 Node.js 版本。这对于开发人员来说意义非凡,因为不同的项目 因为组件包问题 可能需要使用不同的 Node.js 版本,大大提高了开发效率和灵活性。

安装

注意在安装nvm前,系统要尽量保证没有 安装过node(安装过 先卸载调),不然可能出现一些兼容问题,本人实在新电脑中安装的,没有尝试过在此种情况,但网上说有不可预见的问题,有兴趣的小伙伴可以尝试下。

windows

下载链接

选择下方的 nvm-setup.exe 点击下载

image.png

下载完成后,运行 nvm-setup.exe 文件

选择 我接受协议(i accept the agreeement),然后一路点下一步(next),完成(finsh)后

运行命令行工具,可以是 cmd 可以是 Git Bash 执行 nvm -v 提示以下信息说明安装成功!

$ nvm -v
1.1.11
 

mac

mac 有两种安装模式,1. brew 下载,2 我只尝试了第一种,

brew install nvm

扫描二维码关注公众号,回复: 15270362 查看本文章
$ brew install nvm
Warning: You are using macOS 13.
We do not provide support for this pre-release version.
You will encounter build failures with some formulae.
Please create pull requests instead of asking for help on Homebrew's GitHub,
Twitter or any other official channels. You are responsible for resolving
any issues you experience while you are running this
pre-release version.

==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/nvm-0.38.0.all.bottle.tar.gz
curl: (22) The requested URL returned error: 404

Warning: Bottle missing, falling back to the default domain...
==> Downloading https://ghcr.io/v2/homebrew/core/nvm/manifests/0.38.0
Already downloaded: /Users/wanghao/Library/Caches/Homebrew/downloads/d4719fbfd26ca664d78c29b5c8c92ffd0e72cb3a984a968cd3073691e1e3559a--nvm-0.38.0.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/nvm/blobs/sha256:f3cd481b024f9e46a56450d6aaf43c83875e8e7796db6838a6441195b039a368
Already downloaded: /Users/wanghao/Library/Caches/Homebrew/downloads/37a2f0c239860a29bf8ed3deb2ae0c213357e3f96b3b2e68cc67cb2915d6ce67--nvm--0.38.0.all.bottle.tar.gz
==> Pouring nvm--0.38.0.all.bottle.tar.gz
==> Caveats
Please note that upstream has asked us to make explicit managing
nvm via Homebrew is unsupported by them and you should check any
problems against the standard nvm install method prior to reporting.

You should create NVM's working directory if it doesn't exist:

  mkdir ~/.nvm

Add the following to ~/.zshrc or your desired shell
configuration file:

  export NVM_DIR="$HOME/.nvm"
  [ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"  # This loads nvm
  [ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

You can set $NVM_DIR to any location, but leaving it unchanged from
/usr/local/opt/nvm will destroy any nvm-installed Node installations
upon upgrade/reinstall.

Type `nvm help` for further information.
==> Summary
  /usr/local/Cellar/nvm/0.38.0: 7 files, 176KB

在 ~/.bash_profile 文件中 末尾 添加 以下内容

    export NVM_DIR="$HOME/.nvm"
    [ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"  # This loads nvm
    [ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

然后执行

使用

查看 node可下载版本列表

执行命令 nvm list available 查看 可下载的node 版本列表


         $nvm list available

        |   CURRENT    |     LTS      |  OLD STABLE  | OLD UNSTABLE |
        |--------------|--------------|--------------|--------------|
        |    20.3.0    |   18.16.0    |   0.12.18    |   0.11.16    |
        |    20.2.0    |   18.15.0    |   0.12.17    |   0.11.15    |
        |    20.1.0    |   18.14.2    |   0.12.16    |   0.11.14    |
        |    20.0.0    |   18.14.1    |   0.12.15    |   0.11.13    |
        |    19.9.0    |   18.14.0    |   0.12.14    |   0.11.12    |
        |    19.8.1    |   18.13.0    |   0.12.13    |   0.11.11    |
        |    19.8.0    |   18.12.1    |   0.12.12    |   0.11.10    |
        |    19.7.0    |   18.12.0    |   0.12.11    |    0.11.9    |
        |    19.6.1    |   16.20.0    |   0.12.10    |    0.11.8    |
        |    19.6.0    |   16.19.1    |    0.12.9    |    0.11.7    |
        |    19.5.0    |   16.19.0    |    0.12.8    |    0.11.6    |
        |    19.4.0    |   16.18.1    |    0.12.7    |    0.11.5    |
        |    19.3.0    |   16.18.0    |    0.12.6    |    0.11.4    |
        |    19.2.0    |   16.17.1    |    0.12.5    |    0.11.3    |
        |    19.1.0    |   16.17.0    |    0.12.4    |    0.11.2    |
        |    19.0.1    |   16.16.0    |    0.12.3    |    0.11.1    |
        |    19.0.0    |   16.15.1    |    0.12.2    |    0.11.0    |
        |   18.11.0    |   16.15.0    |    0.12.1    |    0.9.12    |
        |   18.10.0    |   16.14.2    |    0.12.0    |    0.9.11    |
        |    18.9.1    |   16.14.1    |   0.10.48    |    0.9.10    |
        ...

下载执行版本 node

LTS 对用的列就是 node版本

执行命令nvm install 18.16.0 安装 对用版本号的 node

$ nvm install 18.16.0
Downloading node.js version 18.16.0 (64-bit)...
Extracting node and npm...
Complete
npm v9.5.1 installed successfully.


Installation complete. If you want to use this version, type

指定当前使用的node 版本

nvm use 18.16.0

nvm use 18.16.0
Now using node v18.16.0 (64-bit)

查看已下载node版本和当前使用的版本

nvm list nvm use 18.16.0 Now using node v18.16.0 (64-bit)

$ nvm list

  * 18.16.0 (Currently using 64-bit executable)
    16.20.0

最后检查 node版本是否已切换

$ node -v
v18.16.0

常见问题

1 官网地址链接打不开

可是被墙了

解决办法

1> 自备梯子

2> 尝试用迅雷下载一下这个下面链接

nvm-setup.exe

2 执行了切换命令 为啥node -v 还是显示之前的版本

一般是权限问题,命令行工具(cmd、Git Bash)要以超级管理员权限的 打开后 在执行切换命令

image.png

3 切换node 版本后,有些命令咋不能用了

因为不同的node版本 都是独立隔离,同一时间只能用一个版本,且安装的对应的全局依赖包也只限制于当前版本,例如 一些脚手架命令行工具,也只限制于对应的node 版本下使用,当切换版本号后 还要重新下载依赖

猜你喜欢

转载自juejin.im/post/7243263236623384634