sdk@nvm manager multi nodejs
nvm
(Node Version Manager)是一个用于管理多个 Node.js 版本的命令行工具。它允许你在同一台机器上安装和切换多个 Node.js 版本,非常适合需要在不同项目中使用不同 Node.js 版本的开发者。
nvm
Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions
1.nvm 介绍
- 全称:Node Version Manager
- 作用:安装、卸载、切换 Node.js 版本
- 支持平台:
- 官方支持 macOS、Linux(包括 WSL)
- Windows 可用 nvm-windows
2.nvm 安装(以 macOS/Linux 为例)
在终端中运行以下命令安装:
1 | curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash |
基于管道执行安装脚本,退出terminal再打开
curl -h
....
-o, --output <file> Write to file instead of stdout
curl -o- http://xxx.sh |bash
3.常用命令
nvm help
[root@vgt-rocky9 ~]# nvm -h
Node Version Manager (v0.39.7)
Note: <version> refers to any version-like string nvm understands. This includes:
- full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
- default (built-in) aliases: node, stable, unstable, iojs, system
- custom aliases you define with `nvm alias foo`
Any options that produce colorized output should respect the `--no-colors` option.
Usage:
nvm --help Show this message
--no-colors Suppress colored output
nvm --version Print out the installed version of nvm
nvm install [<version>] Download and install a <version>. Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm install`:
-s Skip binary download, install from source only.
-b Skip source download, install from binary only.
--reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>
--lts When installing, only select from LTS (long-term support) versions
--lts=<LTS name> When installing, only select from versions for a specific LTS line
--skip-default-packages When installing, skip the default-packages file if it exists
--latest-npm After installing, attempt to upgrade to the latest working npm on the given node version
--no-progress Disable the progress bar on any downloads
--alias=<name> After installing, set the alias specified to the version specified. (same as: nvm alias <name> <version>)
--default After installing, set default alias to the version specified. (same as: nvm alias default <version>)
nvm uninstall <version> Uninstall a version
nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.
nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.
nvm use [<version>] Modify PATH to use <version>. Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm use`:
--silent Silences stdout/stderr output
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm exec [<version>] [<command>] Run <command> on <version>. Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm exec`:
--silent Silences stdout/stderr output
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm run [<version>] [<args>] Run `node` on <version> with <args> as arguments. Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm run`:
--silent Silences stdout/stderr output
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm current Display currently activated version of Node
nvm ls [<version>] List installed versions, matching a given <version> if provided
--no-colors Suppress colored output
--no-alias Suppress `nvm alias` output
nvm ls-remote [<version>] List remote versions available for install, matching a given <version> if provided
--lts When listing, only show LTS (long-term support) versions
--lts=<LTS name> When listing, only show versions for a specific LTS line
--no-colors Suppress colored output
nvm version <version> Resolve the given description to a single local version
nvm version-remote <version> Resolve the given description to a single remote version
--lts When listing, only select from LTS (long-term support) versions
--lts=<LTS name> When listing, only select from versions for a specific LTS line
nvm deactivate Undo effects of `nvm` on current shell
--silent Silences stdout/stderr output
nvm alias [<pattern>] Show all aliases beginning with <pattern>
--no-colors Suppress colored output
nvm alias <name> <version> Set an alias named <name> pointing to <version>
nvm unalias <name> Deletes the alias named <name>
nvm install-latest-npm Attempt to upgrade to the latest working `npm` on the current node version
nvm reinstall-packages <version> Reinstall global `npm` packages contained in <version> to current version
nvm unload Unload `nvm` from shell
nvm which [current | <version>] Display path to installed node version. Uses .nvmrc if available and version is omitted.
--silent Silences stdout/stderr output when a version is omitted
nvm cache dir Display path to the cache directory for nvm
nvm cache clear Empty cache directory for nvm
nvm set-colors [<color codes>] Set five text colors using format "yMeBg". Available when supported.
Initial colors are:
bygre
Color codes:
r/R = red / bold red
g/G = green / bold green
b/B = blue / bold blue
c/C = cyan / bold cyan
m/M = magenta / bold magenta
y/Y = yellow / bold yellow
k/K = black / bold black
e/W = light grey / white
Example:
nvm install 8.0.0 Install a specific version number
nvm use 8.0 Use the latest available 8.0.x release
nvm run 6.10.3 app.js Run app.js using node 6.10.3
nvm exec 4.8.3 node app.js Run `node app.js` with the PATH pointing to node 4.8.3
nvm alias default 8.1.0 Set default node version on a shell
nvm alias default node Always default to the latest available node version on a shell
nvm install node Install the latest available version
nvm use node Use the latest version
nvm install --lts Install the latest LTS version
nvm use --lts Use the latest LTS version
nvm set-colors cgYmW Set text colors to cyan, green, bold yellow, magenta, and white
Note:
to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)
1. 查看可安装的 Node.js 版本:
1 | nvm ls-remote |
2. 安装指定版本:
1 | nvm install 18.17.1 |
加速mirror下载nodejs
NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node nvm install 18
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
3. 查看已安装的版本:
1 | nvm ls |
4. 使用指定版本:
1 | nvm use 18.17.1 |
5. 设置默认版本:
1 | nvm alias default 18.17.1 |
6. 卸载版本:
1 | nvm uninstall 16.20.0 |
~/.nvm结构
4.nvm加速
要为 nvm
安装和使用 Node.js 时 加速下载,你可以通过配置 国内镜像源 来实现,特别适用于中国大陆用户。
1. 使用淘宝源(现在为 npmmirror.com)
阿里团队维护的 npmmirror.com(原淘宝镜像) 提供了 Node.js 和 nvm
镜像加速。
临时加速安装某个 Node.js 版本
1 | NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node nvm install 18 |
这只对当前命令生效。
永久配置加速(推荐)
你可以把镜像地址添加到你的 shell 配置文件里(如 ~/.bashrc
、~/.zshrc
或 ~/.bash_profile
):
1 | export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node |
然后运行:
1 | source ~/.zshrc # 或你使用的其他 shell 配置文件 |
以后你就可以正常使用:
1 | nvm install 20 |
并且它会自动走镜像源。
2.可选:加速 npm 包的安装
Node.js 安装完成后,默认使用的是 npm 官方源,也可以配置为国内镜像源加速:
1 | npm config set registry https://registry.npmmirror.com |
验证是否成功:
1 | npm config get registry |