美文网首页
node版本管理工具nvm-Mac下安装及使用

node版本管理工具nvm-Mac下安装及使用

作者: 二斤寂寞 | 来源:发表于2019-02-19 12:03 被阅读0次

目前主流的node版本管理工具有两种,nvm和n。
管理 node 版本,选择 nvm 还是 n?

1、安装 nvm

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

2、配置 nvm 环境变量

nvm被安装在了~/.nvm
我使用的是zsh,就需要在~/.zshrc这个配置文件中配置
否则就自行配置下~/.bash_profile或者~/.profile

打开~/.zshrc,在最后一行加上:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm

3、让 nvm 生效

source ~/.zshrc重新启动一下配置。
输入nvm可以看到如下信息:

➜  ~  nvm

Node Version Manager

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`

Usage:
  nvm help                                  Show this message
  nvm --version                             Print out the latest released version of nvm
  nvm install [-s] <version>                Download and install a <version>, [-s] from source. Uses .nvmrc if available
    --reinstall-packages-from=<version>     When installing, reinstall packages installed in <node|iojs|node version number>
  nvm uninstall <version>                   Uninstall a version
  nvm use [--silent] <version>              Modify PATH to use <version>. Uses .nvmrc if available
  nvm exec [--silent] <version> [<command>] Run <command> on <version>. Uses .nvmrc if available
  nvm run [--silent] <version> [<args>]     Run `node` on <version> with <args> as arguments. Uses .nvmrc if available
  nvm current                               Display currently activated version
  nvm ls                                    List installed versions
  nvm ls <version>                          List versions matching a given description
  nvm ls-remote                             List remote versions available for install
  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
  nvm deactivate                            Undo effects of `nvm` on current shell
  nvm alias [<pattern>]                     Show all aliases beginning with <pattern>
  nvm alias <name> <version>                Set an alias named <name> pointing to <version>
  nvm unalias <name>                        Deletes the alias named <name>
  nvm reinstall-packages <version>          Reinstall global `npm` packages contained in <version> to current version
  nvm unload                                Unload `nvm` from shell
  nvm which [<version>]                     Display path to installed node version. Uses .nvmrc if available

Example:
  nvm install v0.10.32                  Install a specific version number
  nvm use 0.10                          Use the latest available 0.10.x release
  nvm run 0.10.32 app.js                Run app.js using node v0.10.32
  nvm exec 0.10.32 node app.js          Run `node app.js` with the PATH pointing to node v0.10.32
  nvm alias default 0.10.32             Set default node version on a shell

Note:
  to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)

使用切换node版本 nvm文档

1、查看node有哪些版本可以安装

nvm ls-remote

  v10.14.2
       v10.15.0
       v10.15.1
        v11.0.0
        v11.1.0
        v11.2.0
        v11.3.0
        v11.4.0
        v11.5.0
        v11.6.0
        v11.7.0
        v11.8.0
        v11.9.0
       v11.10.0
            ...

2、安装多个node版本

$ nvm install v11.0.0
######################################################################## 100.0%

Now using node v11.0.0
$ nvm install v11.10.0
######################################################################## 100.0%

Now using node v11.10.0

3、使用nvm轻松切换node版本

在介绍使用方法前,简单说明一下nvm的工作原理:
按照我上述安装方法的话,nvm会将各个版本的node安装在~/.nvm/versions/node目录下,我们可以打开这个目录看看有些什么东西:

➜  ~  ls -a ~/.nvm/versions/node

事实上vXXXvXX这两个目录分别存放node的binary档,nvm会在$PATH前面安插指定版本的目录,透过这种方式在使用node命令时就会用指定版本的node来运行了。
可以确认实际的$PATH看看:

➜  ~  echo $PATH
/Users/***/.nvm/versions/node/xxx/bin:...

由于刚刚我们通过nvm安装node,会自动把最后安装的版本设为当前使用的版本,因此上述路径结尾会是.../v11.10.0/bin(还可通过nvm ls命令查看当前已安装的所有node版本)。

接下来我们可以使用nvm use <version>切换版本:

 nvm use vxxx

3、使用nvm设置默认node版本

➜  ~  nvm alias default v11.10.0
default -> v11.10.0

此时再打开一个bash输入nvm current就会显示为v11.10.0

参考文章:
https://segmentfault.com/a/1190000004404505

相关文章

  • Mac上使用nvm管理node版本的操作记录

    参考文章:1.node版本管理工具nvm-Mac下安装及使用2.正确的安装和使用nvm 关于安装和使用nvm,参考...

  • 升级node版本

    查看node版本,没安装的请先安装 清楚node缓存; 安装node版本管理工具'n'; 使用版本管理工具安装指定...

  • node版本管理工具nvm-Mac下安装及使用

    目前主流的node版本管理工具有两种,nvm和n。管理 node 版本,选择 nvm 还是 n? 1、安装 nvm...

  • node mac升级 版本控制

    安装方法: 首先查看 node 版本; 清楚 node 缓存; 安装 node 版本管理工具 'n'; 使用版本管...

  • node版本更新

    查看当前node版本 清除cache 安装node版本管理工具——"n" 安装最新稳定版node 查看node版本...

  • Node 开发环境搭建和基本使用

    搭建 Node 开发环境 我们需要安装和管理多个 Node 版本,可使用的 Node 版本管理工具有 n 和 nv...

  • 使用nvm安装Node.js环境

    安装 nvm nvm 是node的版本管理工具,我们使用nvm来切换Node.js版本。 nvm的GitHub地址...

  • 前端开发工具版本升级

    当前版本查看 node -vnpm -vyarn -v node 安装node版本管理工具nsudo npm in...

  • nvm避坑指南

    nvm node version manager(node版本管理工具) 通过将多个node 版本安装在指定路径,...

  • 使用n升级node版本

    安装及使用 安装 查看已安装版本 安装稳定的官方版本 安装最新的LTS官方版本 安装最新的node版本 使用某个版...

网友评论

      本文标题:node版本管理工具nvm-Mac下安装及使用

      本文链接:https://www.haomeiwen.com/subject/aagseqtx.html