Node 工具使用(NVM、npm、npx、yarn 和 pnpm)
NVM
NVM 是 Node.js 的版本管理工具。
安装 & 更新
NVM 官方不建议使用 Homebrew 管理 nvm。
无论是安装还是更新 nvm,都是运行下面的安装脚本:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
如果要在 Windows 上安装 nvm,参考 Install nvm-windows
常用命令
命令 | 说明 |
---|---|
nvm install <node-version> |
安装指定版本的 Node.js。 |
nvm uninstall <node-version> |
删除指定版本的 Node.js。 |
nvm use <node-version> |
切换使用指定的版本。 |
nvm ls-remote --lts |
列出所有官方的 Node.js 版本。 |
nvm ls |
列出所有安装的 Node.js 版本。 |
nvm current |
显示当前使用的版本。 |
nvm alias |
给不同版本添加别名。 |
nvm unalias |
删除自定义的别名。 |
nvm reinstall-packages |
在当前 Node.js 环境下,重新安装指定版本号的 npm 包。 |
默认别名:
default
:默认启用的 Node.js 版本。node
:通常指向最新的稳定版本的 Node.js。当你运行nvm install node
时,nvm 会安装最新的稳定版,并将其作为node
别名。stable
:通常指向最新的稳定版本的 Node.js,与node
类似。lts/*
:指向最新的长期支持(LTS)版本的 Node.js,例如lts/argon
。lts/argon
、lts/boron
等:指向特定代号的 LTS 版本。
nvm install node # 安装最新的稳定版本的 node
nvm install 16 # 安装最新版的 node 16
nvm alias default 16 # 设置默认使用 node 16
node 版本选择:对于老项目,最好使用
16.x.x
版本。更高级的版本有很大的兼容性问题。
Troubleshooting
在 Windows 上执行 nvm use <node-version>
命令出现乱码:原因是权限不足。请在管理员权限下运行这条命令。
npm
常用命令
npm init # 初始化项目,生成 package.json 文件
npm install # 安装 package.json 中的依赖
# 安装生产依赖(默认)
npm install <package> --save
npm install <package> -S
# 安装开发依赖
npm install xxx --save-dev
npm install xxx -D
换源
npm 换源有多种方法
使用中国 npm 镜像(建议)
# 设置永久镜像源
npm config set registry https://registry.npmmirror.com/
# 也可以临时使用镜像源安装一个包
npm install <package> --registry=https://registry.npmmirror.com
你也可以为只单个项目指定镜像源,在项目根目录下创建文件
.npmrc
并填写配置
使用镜像管理工具 nrm
# 安装 nrm
npm install -g nrm
# 设置镜像源
nrm add npmmirror https://registry.npmmirror.com
nrm add official https://registry.npmjs.org
# 切换镜像源
nrm use npmmirror
使用淘宝镜像源包管理器 cnpm
# 安装 cnpm
npm install -g cnpm --registry=https://registry.npmmirror.com
# 使用 cnpm 安装包
cnpm install <package>
cnpm
和npm
不要混用最好换源而不是使用
cnpm
Troubleshooting
Only Mac 64 bits supported
npm ERR! code 1
npm ERR! path /Users/xxx/node_modules/chromedriver
npm ERR! command failed
npm ERR! command sh -c node install.js
npm ERR! Only Mac 64 bits supported.
解决方法:
npm install --ignore-scripts
参考:解决 npm install 时报错无法安装 chromedriver 的问题 | 51CTO 博客
npm 代理问题
npm ERR! code ECONNRESET
npm ERR! errno ECONNRESET
npm ERR! network request to https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz failed, reason: Client network socket disconnected before secure TLS connection was established
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly. See: 'npm help config'
npm ERR! A complete log of this run can be found in:
npm ERR! /home/xxx/.npm/_logs/2022-10-22T11_00_36_894Z-debug-0.log
解决方法:关闭终端代理,或者设置 npm 代理,或者使用 HTTP (而不是 HTTPS) 连接 package 库(不建议):
# 设置 npm 代理
npm config set proxy=http://127.0.0.1:7890
# 或者使用 HTTP 连接
npm config set registry=http://registry.npmjs.org
最好设置镜像源而不是设置代理
npx
npx
是一个随 npm 一起发布的命令行工具。npx
的设计初衷是为了简化使用 npm 包的一些常见任务,尤其是那些不需要全局安装的命令行工具。
-
比如,有时你可能只想在项目中安装一个包,而不希望全局安装。使用
npx
可以直接运行本地安装的包,而无需手动指定路径。例如,如果你在项目中本地安装了
ts-node
:npm install ts-node
你可以使用
npx
运行ts-node
而不需要全局安装它:npx ts-node main.ts
-
或者有时你可能只需要运行一次某个工具,而不希望安装它。
npx
会临时下载并运行该工具,然后删除它。例如,你想检查一个包的版本,但不想安装
npm-check
:npx npm-check
-
npx
还支持运行远程代码片段,例如 GitHub Gist:npx gist.github.com/username/gistid
Yarn 2 和 pnpm
yarn 2 和 pnpm 都是使用 Corepack 管理的,Corepack 是一个管理包管理器的工具。你可以在 Corepack 官网 查看相关介绍。
首先启用 Corepack:
corepack enable
Yarn 2
使用 yarn 2 初始化项目:
yarn init -2
常用命令:
yarn set version stable # 更新 yarn 为最新的稳定版
yarn install # npm install
yarn add <package> # npm install <package>
pnpm
corepack prepare pnpm@latest --activate
常用命令:
pnpm install # npm install
pnpm add <pkg> # npm install <package>
pnpm <cmd> # npm run <cmd>