使用nvm安装nodejs

4次阅读

共计 1742 个字符,预计需要花费 5 分钟才能阅读完成。

macOS 系统和 windows 系统安装 nodejs,部分 linux 可以参考 macOS 的安装方式。

我的博客文章:https://blog.ci0n.cn/p_e8204359.html

安装 nvm

nvm 是 nodejs 的版本管理工具,可以快速切换更新 nodejs 版本
windows 系统使用 nvm-windows。

curl 下载:

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

wget 下载:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

检查环境变量的配置:

cat ~/.bash_profile

如果出现以下内容则是配置好了,否则请加以下内容添加到 .bash_profile 文件中:

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

添加完后需要重启脚本:

source ~/.bash_profile

使用 zsh 需要注意,每次打开新的终端窗口都会无法使用 nvm 命令,需要把 .bash_profile 添加到 .zshrc 文件中:

  1. 打开 ~/.zshrc 文件
  2. 找到 # User configuration 所在的行
  3. 在下方添加source ~/.bash_profile
  4. 保存退出后重新执行脚本source ~/.zshrc

验证是否安装完成:

nvm --version

windows 安装

打开 nvm-windows 库的 releases,下载最新版的 nvm-setup.zip
安装时会需要选择两个安装目录,一个是 nvm 的目录,另一个是 node 的版本库目录,我这里分别用 C:\nvmC:\nvm\nodejs

安装完后打开 C:\nvm\setting.txt 添加以下内容,切换 node 和 npm 的下载镜像,国内会快很多:

node_mirror: https://npm.taobao.org/mirrors/node/
npm_mirror: https://npm.taobao.org/mirrors/npm/

检查是否安装完成:

nvm version

安装 nodejs

nvm安装完后就可以开始管理 nodejs 的版本了

# 最新的 lts 版本
nvm install --lts

# windows 需要准确的版本号才可以下载(v 要不要都可以)
nvm install v10.16.3

安装完后需要切换 node 版本:

# macOS 系统的 use 指令只是临时切换
nvm alias default v10.16.3

# windows 在首次安装完后必须执行该命令,否则无法找到 node 命令,nvm use v10.16.3

配置淘宝镜像:

npm config set registry https://registry.npm.taobao.org --global 
npm config set disturl https://npm.taobao.org/dist --global

验证是否安装完成:

npm -v
npx -v
node -v

nvm 常用命令

nvm install --lts # 下载最新的稳定版
nvm use < 版本号 > # 临时切换版本
nvm alias default < 版本号 > #永久切换版本(版本别名,default 就是默认使用的版本)nvm uninstall < 版本号 > # 删除指定版本
nvm ls # 查看本地所有版本
nvm ls-remote --lts # 查看线上所有稳定版 

windows:

nvm install < 版本号 > # 下载指定版本
nvm use < 版本号 > # 切换版本
nvm uninstall < 版本号 > # 删除指定版本
nvm list available # 查看线上所有版本

node 常用命令

npm init # 初始化 node 环境,- y 可以快速初始化
npm i <package> # 下载指定库,看情况添加 - S 或者 -D,全局安装 -g
npm uninstall <package> # 删除 node 包,删除全局 -g
npm list --depth 0 # 查看当前目录下载的 node 包
npm list -g --depth 0 # 查看全局安装的 node 包

正文完
 0