(重要)nvm(node.js version management) 与 node版本/ 使用nvm安装多版本nodejs/配置环境变量

node下载:

下载地址 : https://nodejs.org/zh-cn/download/

如果有nvm先卸载nvm再安装node

参考:

菜鸟教程 : 使用 nvm 管理不同版本的 node 与 npm

Node版本的升级和降级

nvm安装与使用

一、nvm是什么

nvm全名node.js version management,顾名思义是一个nodejs的版本管理工具。

通过它可以安装和切换不同版本的nodejs。下面列出下载、安装及使用方法。

二、windows下载/安装

可在点此在github上下载最新版本,本次下载安装的是windows版本。打开网址我们可以看到有两个版本:

  • nvm-noinstall.zip:绿色免安装版,但使用时需进行配置。
  • nvm-setup.zip:安装版,推荐使用

安装完验证:

ablexq:~ tmk$ nvm

Node Version Manager (v0.37.0)

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
  nvm --version                               Print out the installed version of nvm
  
  nvm install [<version>]                     下载并安装<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.
    --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>                     卸载版本
  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>]                         修改PATH以使用<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                                 显示节点的当前激活版本
  
  nvm ls [<version>]                          列出已安装的版本, matching a given <version> if provided
    --no-colors                               Suppress colored output
    --no-alias                                Suppress `nvm alias` output
  nvm ls-remote [<version>]                   列出可用于安装的远程版本, 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>                       将给定的描述解析为单个本地版本
  nvm version-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>]             显示已安装节点版本的路径. Uses .nvmrc if available and version is omitted.
    --silent                                  Silences stdout/stderr output when a version is omitted
  nvm cache dir                               显示nvm缓存目录的路径
  nvm cache clear                             nvm的空缓存目录

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

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

三、安装/管理nodejs

1、查看本地安装的所有版本;有可选参数available,显示所有可下载的版本。

nvm list [available]

# 

nvm ls

2、安装,命令中的版本号可自定义,具体参考命令1查询出来的列表

nvm install 11.13.0

3、使用特定版本

nvm use 11.13.0

4、卸载

nvm uninstall 11.13.0

四、mac安装配置:

# 卸载
ablexq:~ tmk$ brew uninstall nvm
Uninstalling /usr/local/Cellar/nvm/0.35.0... (7 files, 146.4KB)


# 安装
ablexq:~ tmk$ brew install nvm
==> Downloading https://github.com/creationix/nvm/archive/v0.37.0.tar.gz
Already downloaded: /Users/tmk/Library/Caches/Homebrew/downloads/6751302114cd390789c662ca013466778ef0ee197b7c98be9411ea4f28562b06--nvm-0.37.0.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 /Users/tmk/.bash_profile 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.

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Summary
🍺  /usr/local/Cellar/nvm/0.37.0: 7 files, 162.2KB, built in 6 seconds
ablexq:~ tmk$ 

配置:

mkdir ~/.nvm
open -e .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

验证:

# 验证一:
ablexq:~ tmk$ nvm ls
               
->       system
iojs -> N/A (default)
node -> stable (-> N/A) (default)
unstable -> N/A (default)


# 验证二:
ablexq:~ tmk$ nvm -v
0.37.0
ablexq:~ tmk$ 

卸载Mac电脑上的node

homebrew 安装的

brew uninstall node

官网下载pkg安装包的

sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}

uninstallnode.sh 脚本卸载

#!/bin/bash
lsbom -f -l -s -pf /var/db/receipts/org.nodejs.pkg.bom \
| while read i; do
  sudo rm /usr/local/${i}
done
sudo rm -rf /usr/local/lib/node \
     /usr/local/lib/node_modules \
     /var/db/receipts/org.nodejs.*

修改文件权限 chmod 777 uninstallNodejs.sh
在命令行执行

这些东西删完了,node就算删除了。
但是还有好多基于node安装的一堆软件和命令行工具,也需要重新安装,例如 react-native, supervisor,pm2 etc
需要删除/usr/local/bin 下面相关的文件,其实它们只是些软连接,正主都在 /usr/local/lib/node_modules/ 目录下。
在之前的步骤中,已经被删除了,但是按 tab键还能找到,就是因为这些软连接还存在

常用示例:

1、 安装nvm

brew uninstall nvm
brew install nvm

2、配置环境变量

mkdir ~/.nvm
open -e .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

3、验证

nvm ls

4、安装并使用指定版本的node

nvm install v14.2.0

# 切换使用指定版本
nvm use v14.2.0

m1

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

.bash_profile配置:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Node.js是一个基于Chrome V8引擎的JavaScript运行环境,可以让JavaScript脱离浏览器运行在服务器端。nvmNode.js版本管理工具,可以方便地切换不同版本Node.js。 以下是Node.jsnvm安装及环境配置步骤: 1. 安装Node.js 可以在Node.js官网下载对应操作系统的安装包进行安装,也可以使用包管理器进行安装。例如,在Ubuntu系统中可以使用以下命令进行安装: ``` sudo apt-get update sudo apt-get install nodejs ``` 2. 安装nvm 可以在nvm的GitHub仓库中下载安装脚本进行安装。例如,在Linux系统中可以使用以下命令进行安装: ``` curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash ``` 3. 配置环境变量 为了方便使用Node.jsnvm,需要将它们的路径添加到系统的环境变量中。在Linux系统中,可以编辑~/.bashrc文件,在文件末尾添加以下内容: ``` export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion ``` 保存文件后执行以下命令使配置生效: ``` source ~/.bashrc ``` 4. 使用nvm安装Node.js 使用nvm安装Node.js非常方便,只需要执行以下命令即可: ``` nvm install <version> ``` 其中,<version>是Node.js版本号,例如: ``` nvm install 14.17.6 ``` 5. 使用nvm切换Node.js版本 使用nvm可以方便地切换不同版本Node.js。执行以下命令可以列出已安装Node.js版本: ``` nvm ls ``` 执行以下命令可以切换到指定版本Node.js: ``` nvm use <version> ``` 例如,切换到14.17.6版本Node.js: ``` nvm use 14.17.6 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值