002 vue3-admin项目的目录及文件说明之.npmrc文件
说明
.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
8. node-linker
取值范围

工作原理
├── node_modules/ │ ├── package-a/ # 直接依赖 │ ├── package-b/ # 直接依赖 │ ├── dependency-x/ # 被提升的间接依赖 │ └── dependency-y/ # 被提升的间接依赖 ├── package.json └── .npmrc
配置示例
# .npmrc node-linker=hoisted
依赖提升机制详解
依赖提升规则 1 版本冲突解决:当多个包依赖同一个包的不同版本时,npm 会: 将最高版本提升到顶层 其他版本保留在各自包的 node_modules 中 2 重复依赖消除:相同版本的依赖只会在顶层出现一次
项目级 .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}

浙公网安备 33010602011771号