安装

Github安装链接

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

Gitee安装链接(含设定淘宝镜像源,适用中国大陆)

curl -o- https://gitee.com/RubyMetric/nvm-cn/raw/main/install.sh | bash

更新nvm

安装时可能使用旧版的nvm程序,可在安装后执行一次更新命令。

nvm update
上述命令执行时,请确保有权限。

例外操作

可能会遭遇受限权限写入或者需要重载环境等情况,通常执行以下命令可恢复正常。

# 更新权限
chmod +x ~/.nvm/nvm.sh
# 重新加载环境变量
source ~/.bashrc

nvm指令

  1. 查看当前机器安装版本

    nvm ls
  2. 查看可以云端版本

    nvm ls-remote
  3. 安装指定版本Node.js

    nvm install [<version>]
  4. 卸载指定Node.js版本

    nvm uninstall [<version>]
  5. 使用指定Node.js版本

    nvm use [<version>]
  6. 当前使用Node.js版本

    nvm current
  7. 在对应项目中切换不同Node.js版本

在项目中创建项目所需要的版本声明

touch .nvmrc
echo "v18.19.0" >> .nvmrc

在指定目录下使用nvm use即可切换到对应版本

也可以使用其他方式在使用npm时自动辨识项目的版本文件进行切换操作。

NVM命令输出选项

原文

Node Version Manager (v0.39.5)

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`)

译文

个人翻译仅供参考,请以自己实际理解为准。

Node 版本管理器 (v0.39.5)

注意:<version> 指的是 nvm 能理解的任何类似版本的字符串。这包括:
  - 完整或部分版本号,以可选的 "v" 开头(0.10, v0.1.2, v1)
  - 内置的默认别名:node, stable, unstable, iojs, system
  - 你通过 `nvm alias foo` 定义的自定义别名

任何产生彩色输出的选项都应该尊重 `--no-colors` 选项。

用法:
  nvm --help                                  显示此消息
    --no-colors                               抑制彩色输出
  nvm --version                               打印已安装的 nvm 版本
  nvm install [<version>]                     下载并安装一个 <version>。如果省略版本号,则使用 .nvmrc(如果可用)。
   如果提供了以下可选参数,则必须直接出现在 `nvm install` 后:
    -s                                        跳过二进制下载,只从源码安装。
    -b                                        跳过源码下载,只从二进制安装。
    --reinstall-packages-from=<version>       安装时,重新安装在 <node|iojs|node 版本号> 中安装的包
    --lts                                     安装时,只选择长期支持(LTS)版本
    --lts=<LTS 名称>                          安装时,只选择特定 LTS 系列的版本
    --skip-default-packages                   安装时,如果存在默认包文件,则跳过
    --latest-npm                              安装后,尝试升级到给定 node 版本上最新可用的 npm
    --no-progress                             禁用任何下载的进度条
    --alias=<名称>                            安装后,将指定的别名设置为指定的版本。 (与 `nvm alias <名称> <版本>` 相同)
    --default                                 安装后,将默认别名设置为指定的版本。 (与 `nvm alias default <版本>` 相同)
  nvm uninstall <version>                     卸载一个版本
  nvm uninstall --lts                         使用 `lts/*` 的自动 LTS(长期支持)别名进行卸载(如果可用)。
  nvm uninstall --lts=<LTS 名称>              使用提供的 LTS 系列的自动别名进行卸载(如果可用)。
  nvm use [<version>]                         修改 PATH 以使用 <version>。如果省略版本号,则使用 .nvmrc(如果可用)。
   如果提供了以下可选参数,则必须直接出现在 `nvm use` 后:
    --silent                                  静默 stdout/stderr 输出
    --lts                                     使用 `lts/*` 的自动 LTS(长期支持)别名(如果可用)。
    --lts=<LTS 名称>                          使用提供的 LTS 系列的自动别名(如果可用)。
  nvm exec [<version>] [<command>]            在 <version> 上运行 <command>。如果省略版本号,则使用 .nvmrc(如果可用)。
   如果提供了以下可选参数,则必须直接出现在 `nvm exec` 后:
    --silent                                  静默 stdout/stderr 输出
    --lts                                     使用 `lts/*` 的自动 LTS(长期支持)别名(如果可用)。
    --lts=<LTS 名称>                          使用提供的 LTS 系列的自动别名(如果可用)。
  nvm run [<version>] [<args>]                在 <version> 上用 <args> 作为参数运行 `node`。如果省略版本号,则使用 .nvmrc(如果可用)。
   如果提供了以下可选参数,则必须直接出现在 `nvm run` 后:
    --silent                                  静默 stdout/stderr 输出
    --lts                                     使用 `lts/*` 的自动 LTS(长期支持)别名(如果可用)。
    --lts=<LTS 名称>                          使用提供的 LTS系列的自动别名(如果可用)。
  nvm current                                 显示当前激活的 Node 版本
  nvm ls [<version>]                          列出安装的版本,如果提供了 <version> 则匹配给定版本
    --no-colors                               抑制彩色输出
    --no-alias                                抑制 `nvm alias` 输出
  nvm ls-remote [<version>]                   列出可供安装的远程版本,如果提供了 <version> 则匹配给定版本
    --lts                                     列出时,只显示长期支持(LTS)版本
    --lts=<LTS 名称>                          列出时,只显示特定 LTS 系列的版本
    --no-colors                               抑制彩色输出
  nvm version <version>                       将给定描述解析为单个本地版本
  nvm version-remote <version>                将给定描述解析为单个远程版本
    --lts                                     列出时,只从长期支持(LTS)版本中选择
    --lts=<LTS 名称>                          列出时,只从特定 LTS 系列的版本中选择
  nvm deactivate                              撤销 `nvm` 对当前 shell 的影响
    --silent                                  在版本省略时静默 stdout/stderr 输出
  nvm alias [<pattern>]                       显示以 <pattern> 开头的所有别名
    --no-colors                               抑制彩色输出
  nvm alias <name> <version>                  设置一个名为 <name> 的别名,指向 <version>
  nvm unalias <name>                          删除名为 <name> 的别名
  nvm install-latest-npm                      尝试在当前 node 版本上升级到最新可用的 `npm`
  nvm reinstall-packages <version>            将 <version> 中包含的全局 `npm` 包重新安装到当前版本
  nvm unload                                  卸载 shell 中的 `nvm`
  nvm which [current | <version>]             显示安装的 node 版本的路径。如果省略版本号,则使用 .nvmrc(如果可用)。
    --silent                                  当版本省略时静默 stdout/stderr 输出
  nvm cache dir                               显示 nvm 缓存目录的路径
  nvm cache clear                             清空 nvm 的缓存目录
  nvm set-colors [<颜色代码>]                  使用格式 "yMeBg" 设置五种文本颜色。当支持时可用。
                                               初始颜色为:
                                                  bygre
                                               颜色代码:
                                                r/R = 红色 / 加粗红色
                                                g/G = 绿色 / 加粗绿色
                                                b/B = 蓝色 / 加粗蓝色
                                                c/C = 青色 / 加粗青色
                                                m/M = 品红色 / 加粗品红色
                                                y/Y = 黄色 / 加粗黄色
                                                k/K = 黑色 / 加粗黑色
                                                e/W = 浅灰色 / 白色
示例:
  nvm install 8.0.0                     安装特定版本号
  nvm use 8.0                           使用最新可用的 8.0.x 版本
  nvm run 6.10.3 app.js                 使用 node 6.10.3 运行 app.js
  nvm exec 4.8.3 node app.js            用指向 node 4.8.3 的 PATH 运行 `node app.js`
  nvm alias default 8.1.0               在 shell 上设置默认 node 版本
  nvm alias default node                始终默认在 shell 上使用最新可用的 node 版本

  nvm install node                      安装最新可用版本
  nvm use node                          使用最新版本
  nvm install --lts                     安装最新的 LTS 版本
  nvm use --lts                         使用最新的 LTS 版本

  nvm set-colors cgYmW                  将文本颜色设置为青色、绿色、加粗黄色、洋红色和白色

注意:
  要移除、删除或卸载 nvm - 只需删除 `$NVM_DIR` 文件夹(通常在 `~/.nvm`)

最后修改:2024-01-12
如果觉得我的文章有意思,欢迎赞赏,我会努力创造更好的文章!