本文整理了 NPM 和 Node.js 开发中的常用配置技巧。

NPM 镜像配置

国内使用 NPM 下载包较慢,可以切换到国内镜像加速:

临时使用

1
npm install <package> --registry=https://registry.npmmirror.com

永久配置

1
2
3
4
5
6
7
8
9
10
11
# 设置淘宝镜像(推荐)
npm config set registry https://registry.npmmirror.com

# 或使用 cnpm 镜像
npm config set registry http://registry.cnpmjs.org

# 查看当前配置
npm config get registry

# 恢复官方源
npm config set registry https://registry.npmjs.org

使用 nrm 管理多个源

1
2
3
4
5
6
7
8
9
10
11
# 安装 nrm
npm install -g nrm

# 查看可用源
nrm ls

# 切换源
nrm use taobao

# 测试速度
nrm test

参考: 重置npm镜像代理,提高node包下载速度


常用 NPM 命令

命令 说明
npm init 初始化项目
npm install 安装所有依赖
npm install <pkg> --save 安装并添加到 dependencies
npm install <pkg> --save-dev 安装并添加到 devDependencies
npm update 更新依赖
npm outdated 查看过期依赖
npm cache clean --force 清理缓存