Node.js:NVM工具的使用 nvm常用命令

NVM: Node Version Manager

Node.js的版本管理工具,可以实现多个node版本共存,并随意切换,就相当于python里面的pyenv

项目地址:https://github.com/nvm-sh/nvm/blob/master/README.md

一 安装

1.1 Mac 安装 nvm

https://github.com/nvm-sh/nvm/blob/master/README.md

1.2 Windows 安装 nvm

nvm-windows
nodist

二 使用

① nvm help 翻译解释

首先我们看下帮助文档,其实这个文档说的很明白了

nvm help 

Node Version Manager (v0.39.1)

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版本)
  nvm install [<version>]                     Download and install a <version>. Uses .nvmrc if available and version is omitted.(下载指定的node版本,这个是常用命令)
   The following optional arguments, if provided, must appear directly after `nvm install`(这些参数必须紧紧跟在install的后面,比如vnm install -s 8.0.0):
    -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(指定LTS的名字)
    --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(node安装完成后,自动更新到最新版的npm)
    --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.(直接在某个版本上运行node脚本)
   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(查看当前的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(列出可以被下载的所有的node版本)
    --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(撤销' nvm '对当前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(在当前的node版本中,吧npm更新为最新)
  nvm reinstall-packages <version>            Reinstall global `npm` packages contained in <version> to current version(从新安装某个版本)
  nvm unload                                  Unload `nvm` from shell(从shell卸载' nvm ')
  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:
                                                   g  b  y  r  e
                                               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`)

② NVM常用命令

1、查找版本:nvm ls 、 ls-remote

nvm ls 列出已经本地已经安装的版本

nvm ls

在这里插入图片描述
nvm ls-remote 列出云端所有能被安装的版本

2、安装命令: nvm install

安装指定版本的node,该命令有多个参数 ,参数应该紧跟在install之后,参数参考上面help翻译
直接vnm install加版本号即可,也可以只写主版本号,只锁定主版本号,次版本取最新
在这里插入图片描述

不推荐用-s 这个选项表示是从源码编译

vnm install -s 8.0.0

3、卸载命令:nvm uninstall

注意:删除某个版本时,一定要切换到其他版本,否者无法卸载

nvm uninstall 8.0.0
Uninstalled node v8.0.0

4、查看版本:nvm current

nvm current 
v8.0.0

5、切换版本:nvm use

通过别名切换

nvm use default 
Now using node v16.15.0 (npm v8.5.5)

通过版本切换

nvm use 8          
Now using node v8.0.0 (npm v5.0.0)

5、版本别名 nvm alias

1 将8.0.0版本命名为usural

nvm alias usural 8.0.0
usural -> 8.0.0 (-> v8.0.0)

在这里插入图片描述
2 模糊查找 us

nvm alias us
usural -> 8.0.0 (-> v8.0.0)

3 取消别名

nvm unalias usural  
Deleted alias usural - restore it with `nvm alias "usural" "8.0.0"`

6、取消vnm:nvm deactivate

执行命令后,node版本为系统原本的版本

 nvm deactivate --silent

在这里插入图片描述

7、查看本地文件: nvm which [current | ]

nvm which current  
/Users/anaymous/.nvm/versions/node/v8.0.0/bin/node

8、缓存文件:nvm cache

显示缓存文件目录

 nvm cache dir
/Users/anaymous/.nvm/.cache

清除缓存文件

nvm cache clear 
nvm cache cleared.

9、更改显示颜色:nvm set-colors

就是用于更改下图中红红绿绿的那些东西,纯属娱乐的命令,鼓捣玩一玩就行了
gbyre的含义,参考上面的帮助

nvm set-colors gbyre

效果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜘蛛da侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值