npm 命令使用文档

发布于:2025-03-21 ⋅ 阅读:(36) ⋅ 点赞:(0)

目录

  1. 简介
  2. 安装与配置
  3. 基础命令
  4. 依赖管理
  5. 版本控制
  6. 脚本管理
  7. 包发布
  8. 高级命令
  9. 配置管理
  10. 最佳实践
  11. 常见问题

1. 简介

npm (Node Package Manager) 是 Node.js 的官方包管理工具,提供:

  • 130万+ 开源包的注册表访问
  • 依赖解析与版本管理
  • 项目脚本自动化
  • 私有包管理能力
  • 完整的包生命周期管理

2. 安装与配置

2.1 安装Node.js

# 通过官方安装包
https://nodejs.org

# 验证安装
node -v
npm -v

2.2 配置初始化

npm init            # 交互式创建package.json
npm init -y         # 快速生成默认配置

2.3 镜像源配置

npm config set registry https://registry.npmmirror.com  # 阿里云镜像
npm config get registry            # 查看当前源
npm config delete registry         # 恢复默认源

3. 基础命令

3.1 包安装

npm install                         # 安装所有依赖
npm install lodash                  # 安装生产依赖
npm install eslint -D               # 安装开发依赖
npm install react@18.2.0            # 安装指定版本
npm install ../my-package           # 安装本地包

3.2 包管理

npm uninstall axios                 # 卸载包
npm update                          # 更新所有依赖
npm outdated                        # 检查过时依赖
npm ls                              # 查看依赖树
npm cache clean --force             # 清理缓存

4. 依赖管理

4.1 依赖类型

{
  "dependencies": {         // 生产环境依赖
    "lodash": "^4.17.21"
  },
  "devDependencies": {      // 开发环境依赖
    "webpack": "^5.75.0"
  },
  "peerDependencies": {     // 宿主环境依赖
    "react": ">=16.8.0"
  },
  "optionalDependencies": { // 可选依赖
    "fsevents": "^2.3.2"
  }
}

4.2 全局安装

npm install -g typescript       # 全局安装
npm list -g --depth=0           # 查看全局安装包

5. 版本控制

5.1 语义化版本 (SemVer)

^1.2.3  # 兼容次要版本和补丁 (1.x.x)
~1.2.3  # 仅兼容补丁版本 (1.2.x)
1.2.x   # 指定次要版本
*       # 最新版本

5.2 版本锁定

npm shrinkwrap          # 生成npm-shrinkwrap.json
npm ci                  # 严格按lockfile安装

6. 脚本管理

6.1 基础脚本

{
  "scripts": {
    "start": "node index.js",
    "test": "jest",
    "build": "webpack --mode production",
    "prepublish": "npm run build"
  }
}

6.2 高级用法

npm run test -- --coverage      # 传递参数
npm run lint & npm run build    # 并行执行
npm run prestart                # 生命周期钩子

7. 包发布

7.1 发布流程

npm login                       # 登录账号
npm publish                     # 发布公开包
npm publish --access public     # 明确发布公开包
npm version patch               # 版本号升级
npm deprecate <pkg>@<version> "message"  # 标记弃用

7.2 私有包

npm init --scope=yourorg        # 创建组织包
npm publish --access restricted # 发布私有包

8. 高级命令

8.1 审计与安全

npm audit                       # 安全审计
npm audit fix                   # 自动修复漏洞
npm fund                        # 查看依赖资金信息

8.2 调试工具

npm view react                  # 查看包信息
npm docs lodash                 # 打开文档网站
npm repo webpack                # 打开源码仓库
npm explore react -- npm ls     # 进入包目录

9. 配置管理

9.1 配置文件

.npmrc 优先级:
项目级 > 用户级 > 全局 > npm内置

9.2 常用配置项

npm config set save-exact true      # 精确版本
npm config set script-shell bash    # 指定脚本shell
npm config set engine-strict true   # 严格引擎检查

10. 最佳实践

  1. 使用 npm ci 代替 npm install 在CI环境
  2. 定期执行 npm outdatednpm update
  3. 提交 package-lock.json 到版本控制
  4. 使用 npm audit 进行安全审计
  5. 为CLI工具添加 bin 字段
  6. 使用 .npmignore 控制发布内容
  7. 合理使用 peerDependencies 避免重复依赖
  8. 对私有包使用作用域 (@org/package)

11. 常见问题

Q1: 安装权限错误

# 解决方案:
sudo chown -R $(whoami) ~/.npm
# 或使用Node版本管理工具(nvm)

Q2: 依赖冲突解决

npm ls <package-name>        # 查看依赖路径
npm dedupe                   # 尝试优化依赖树

Q3: 加速安装

npm install --prefer-offline # 优先使用缓存
npm config set prefer-offline true  # 永久设置

转载吱一声~