npm 命令速查表
npm 是 Node.js 生态的包管理器核心工具。本速查表覆盖了日常开发中最常用的 npm 命令,从包安装到发布管理,每个命令都有实际使用示例。无论你是刚开始使用 npm 还是需要快速查阅某个命令,这份速查表都能帮到你。
使用场景速查
| 你要做什么 | 用哪个命令 |
| 安装项目依赖 | npm install |
| CI 环境安装 | npm ci |
| 安装一个包 | npm install lodash |
| 安装全局工具 | npm install -g typescript |
| 运行脚本 | npm run dev |
| 更新所有包 | npm update |
| 发布包 | npm publish |
| 查看已安装包 | npm list --depth=0 |
安装管理
``bash
# 安装项目所有依赖
npm install # 根据 package.json 安装(简写: npm i)
npm ci # 严格按 lock 文件安装(CI 环境推荐)
npm install --production # 只安装 dependencies,跳过 devDependencies
# 安装特定包 npm install lodash # 添加到 dependencies npm install -D jest # 添加到 devDependencies (-D = --save-dev) npm install -g typescript # 全局安装 npm install lodash@4.17.21 # 指定版本 npm install lodash@^4.0.0 # 使用 semver 范围
# 卸载 npm uninstall lodash # 从 dependencies 移除 npm uninstall -g typescript # 卸载全局包
# 清理
rm -rf node_modules && npm install # 重装所有依赖(修复问题常用)
npm cache clean --force # 清除缓存(极少需要)
`
> 提示: rm -rf node_modules && npm install 是解决"莫名其妙的 bug"的第一良方。
发布管理
`bash
# 初始化新项目
npm init # 交互式创建 package.json
npm init -y # 使用默认值快速创建
# 版本号管理 npm version patch # 1.0.0 → 1.0.1(修复 bug) npm version minor # 1.0.0 → 1.1.0(新增功能,向后兼容) npm version major # 1.0.0 → 2.0.0(不兼容变更)
# 发布 npm publish # 发布到 registry(默认 npmjs.com) npm publish --tag beta # 发布 beta 标签(用于测试版) npm publish --access public # 发布公开包(作用域包需要)
# 弃用与下架 npm deprecate pkg@"
# 登录
npm login # 登录 npm 账号
npm whoami # 查看当前登录用户
npm logout # 登出
`
> 版本号规范: major.minor.patch — 如 2.1.3。patch 修 bug,minor 加功能,major 不兼容变更。
配置管理
`bash
# 查看配置
npm config list # 查看所有配置
npm config get registry # 查看当前 registry
npm config get prefix # 查看全局安装路径
# 修改配置 npm config set registry https://registry.npmmirror.com # 设置镜像源 npm config set registry https://registry.npmjs.org/ # 恢复官方源
# 使用 .npmrc 文件
# 项目级:项目根目录创建 .npmrc
# 用户级:~/.npmrc
# 配置示例:
echo "registry=https://registry.npmmirror.com" > .npmrc
echo "@mycompany:registry=https://npm.mycompany.com" >> .npmrc # 私有包
`
> 国内镜像: 使用 nrm(npm registry manager)快速切换:npx nrm use taobao。
脚本运行
`bash
# package.json scripts 示例
# "scripts": {
# "dev": "next dev",
# "build": "next build",
# "start": "next start",
# "test": "jest",
# "lint": "eslint ."
# }
npm run dev # 运行 dev 脚本 npm run build # 运行 build 脚本 npm start # 运行 start 脚本(run 可省略) npm test # 运行 test 脚本(run 可省略) npm run --silent # 安静模式,减少输出
# 传参 npm run test -- --watch # 传递 --watch 参数给底层命令 npm run build -- --mode production
# 生命周期钩子(自动执行)
# npm run prebuild → npm run build → npm run postbuild
`
> 脚本技巧: npm run 不加脚本名会列出所有可用脚本。npm run dev -- --port 3000 可传参给底层命令。
包管理
`bash
# 更新包
npm update # 更新所有包(遵循 semver 范围)
npm update lodash # 更新指定包
npm update -g # 更新全局包
# 检查过期 npm outdated # 查看可更新的包列表 # 输出: Package Current Wanted Latest Location # lodash 4.17.21 4.17.21 5.0.0 my-app
# 安全审计
npm audit # 扫描漏洞
npm audit fix # 自动修复兼容的漏洞
npm audit fix --force # 强制修复(可能有 breaking change)
npm audit --json # JSON 格式输出(用于 CI 分析)
`
信息查看
`bash
# 查看已安装包
npm list # 树形显示所有包(可能很长)
npm list --depth=0 # 只显示直接依赖
npm list -g --depth=0 # 全局包
npm list lodash # 查看特定包的版本
# 查看包信息 npm view lodash # 查看 npm 上包的完整信息 npm view lodash versions # 查看所有可用版本 npm view lodash dependencies # 查看依赖关系 npm view lodash --json # JSON 格式
# 搜索 npm search react hook # 搜索包(npm 官方 registry) npx npm-search "react hooks" # 更好的搜索体验
# 环境
node -v # 查看 Node 版本
npm -v # 查看 npm 版本
npx --version # 查看 npx 版本
`
常见问题
| 症状 | 原因 | 修复 |
ERR! code EACCES 权限错误 | npm 全局安装路径权限不足 | 用 npm config set prefix ~/.npm-global 或用 nvm 管理 Node |
ERR! code ERESOLVE 依赖冲突 | 包的 semver 范围冲突 | npm install --legacy-peer-deps 或手动调整版本 |
npm install 极慢 | 官方 registry 网络问题 | 切换到国内镜像:npm config set registry https://registry.npmmirror.com |
ERR! code ENOENT 找不到文件 | package.json 缺失或不完整 | 检查 package.json 是否存在,运行 npm init |
| npm audit 大量漏洞 | 依赖链中有旧版本包 | npm audit fix`,关注 breaking change 提示 |
安装管理(7)
| 命令 | 难度 | ||
|---|---|---|---|
npm install安装项目所有依赖 | 基础 | npm install | |
npm ci严格按 lock 文件安装(CI 环境推荐) | 中级 | npm ci | |
npm install <pkg>安装指定包到 dependencies | 基础 | npm install lodash | |
npm install -D安装到 devDependencies | 基础 | npm install -D jest | |
npm install -g全局安装包 | 中级 | npm install -g typescript | |
npm uninstall卸载包 | 基础 | npm uninstall lodash | |
npm cache clean --force清除 npm 缓存 | 中级 | npm cache clean --force |
发布管理(6)
| 命令 | 难度 | ||
|---|---|---|---|
npm init创建 package.json 文件 | 基础 | npm init -y | |
npm version升级包的版本号 | 中级 | npm version minor | |
npm publish发布包到 npm registry | 中级 | npm publish | |
npm deprecate标记某个版本为弃用 | 高级 | npm deprecate pkg@"<1.0.0" "不再维护" | |
npm login登录 npm 账号 | 基础 | npm login | |
npm whoami查看当前登录用户 | 基础 | npm whoami |
配置管理(3)
| 命令 | 难度 | ||
|---|---|---|---|
npm config list查看所有 npm 配置 | 中级 | npm config list | |
npm config set设置 npm 配置项 | 中级 | npm config set registry https://registry.npmmirror.com | |
npm config get获取指定配置项的值 | 中级 | npm config get registry |
脚本运行(3)
| 命令 | 难度 | ||
|---|---|---|---|
npm run运行 package.json 中定义的脚本 | 基础 | npm run dev | |
npm start运行 start 脚本 | 基础 | npm start | |
npm test运行 test 脚本 | 基础 | npm test -- --watch |
包管理(4)
| 命令 | 难度 | ||
|---|---|---|---|
npm update更新所有包(遵循 semver 范围) | 中级 | npm update | |
npm outdated查看可更新的包列表 | 中级 | npm outdated | |
npm audit扫描依赖漏洞 | 中级 | npm audit | |
npm audit fix自动修复兼容的漏洞 | 中级 | npm audit fix |
信息查看(5)
| 命令 | 难度 | ||
|---|---|---|---|
npm list查看已安装的包列表 | 基础 | npm list --depth=0 | |
npm view查看 registry 上的包信息 | 中级 | npm view lodash versions | |
npm search搜索 npm registry 上的包 | 基础 | npm search react hook | |
node -v查看 Node.js 版本 | 基础 | node -v | |
npm -v查看 npm 版本 | 基础 | npm -v |