说明
.npmrc 文件是 npm 包管理器的配置文件,用于自定义 npm 的行为。
它可以存在于多个位置,具有不同的优先级。
文件位置与优先级
1 项目级:./npmrc(项目根目录) 2 用户级:~/.npmrc(用户主目录) 3 全局级:/etc/npmrc(系统级) 4 npm 内置配置
常用配置选项
1 基础配置
# 设置 npm 仓库源 registry=https://registry.npm.taobao.org/# 设置包安装目录 prefix=D:\Install_soft\nodejs\node_global# 设置缓存目录 cache=D:\Install_soft\nodejs\node_cache# 启用/禁用自动安装依赖 auto-install-peers=true# 启用/禁用自动修复依赖 auto-fix=true
2 代理配置
# HTTP 代理 proxy=http://proxy.example.com:8080 https-proxy=http://proxy.example.com:8080# 不使用代理的主机 noproxy=localhost,127001
3 权限与安全配置
# 启用/禁用严格模式 strict-peer-deps=true# 启用/禁用 package-lock.json package-lock=false# 设置依赖版本锁定策略 save-exact=true# 启用/禁用自动生成 package-lock.json lockfile-version=3
4 私有仓库配置
# 私有仓库配置 @company:registry=https://npm.company.com/ //npm.company.com/:_authToken=your_auth_token# 特定作用域的仓库 @scope:registry=https://registry.npmjs.org/
5 安装配置
# 设置默认安装模式 save=true save-dev=true save-optional=true# 设置依赖类型 save-prefix=^# 启用/禁用自动安装 auto-install-peers=true
6 脚本配置
# 启用/禁用脚本执行 ignore-scripts=false# 设置脚本超时时间(毫秒) script-shell=/bin/bash
7 日志配置
# 设置日志级别 loglevel=info# 启用/禁用进度条 progress=true
项目级 .npmrc 示例
# 项目级配置示例
registry=https://registry.npmmirror.com/
@company:registry=https://npm.company.com/
//npm.company.com/:_authToken=${NPM_TOKEN}# 依赖管理
strict-peer-deps=true
save-exact=true
auto-install-peers=true# 性能优化
prefer-offline=true
环境变量支持
# 使用环境变量
//registry.npmjs.org/:_authToken=${NPM_TOKEN}