Node 项目通过 .npmrc 文件指定要安装的依赖项的源代码。
背景
npm
命令运行时,往往通过命令行指定相关配置,最常用的便是使用 --registry
来指定依赖的安装源。
npm install --registry=https://registry.npmmirror.com
同样的效果也可以使用 .npmrc
来实现:
registry=https://registry.npmmirror.com
.npmrc
用法介绍
.npmrc
(NPM Running Configuration)可以指定 npm
命令运行时的配置内容,其中可以设置的配置见官方文档:config。
配置优先级按顺序如下:
- 项目内的配置文件(
/path/to/my/project/.npmrc
) - 用户配置文件(
~/.npmrc
) - 全局配置文件(
$PREFIX/etc/npmrc
) - NPM 内置配置文件(
/path/to/npm/npmrc
)
.npmrc
文件内以键值对的形式(key=value
)设置值:
key=value
数组:
key[]=value
key[]=value
使用 #
或 ;
来注释:
# Comment1
; Comment2
key=value
指定安装源
由于项目下的 .npmrc
优先级最高,并且配置文件只对此项目有效,不会影响其他项目。可以在项目根目录下面新建一个 .npmrc
文件指定安装源。
修改默认 registry
:
registry=https://registry.npmmirror.com
@scope
的依赖包从 https://scope.example.com
安装,其他从 https://registry.npmmirror.com
:
registry=https://registry.npmmirror.com
@scope:registry=https://scope.example.com
设置 SASS 镜像源,效果与 SASS_BINARY_SITE=https://registry.npmmirror.com/-/binary/node-sass npm install node-sass
相同:
sass_binary_site=https://registry.npmmirror.com/-/binary/node-sass
案例代码:https://github.com/mazeyqian/mazey/blob/master/.npmrc
NPM 配置快捷命令
获取当前配置指定值:
npm config get sass_binary_site
# https://registry.npmmirror.com/-/binary/node-sass
npm config get registry
# https://registry.npmmirror.com
设置配置指定值:
npm config set example_key_1 example_value_1
npm config get example_key_1
# example_value_1
显示配置列表:
npm config list
; userconfig
example_key_1 = "example_value_1"
registry = "https://registry.npmjs.org/"
注意
如果想发布 NPM,一定要注意修改 registry
至你想发布的地址,切勿将公司私有包发布到官方库中。
If you then want to publish a package for the whole world to see, you can simply override the
--registry
option for thatpublish
command.
参考
- 淘宝 npm 域名即将切换 && npmmirror 重构升级
- Can I run my own private registry?
- 解决 NPM 安装 node-sass 因为网络问题超时失败的问题
版权声明
本博客所有的原创文章,作者皆保留版权。转载必须包含本声明,保持本文完整,并以超链接形式注明作者后除和本文原始地址:https://blog.mazey.net/2950.html
(完)
上一篇: npm使用国内淘宝镜像的方法(二)
推荐阅读
-
IOS UI 自动化测试实践:pyhton-wda 环境设置篇-Xcode 版本:10.1iphone 版本:12.0.1OS 版本:10.13.6 实践开始 创建一个新目录并从 git 下载 WDA 项目代码。 git clone https://github.com/facebook/WebDriverAgent 并运行初始化脚本。 ./Scripts/bootstrap.sh 出现以下错误信息:原因:Carthage 需要下载相关的依赖项,而这些依赖项并未在本地安装。 解决方法通过 brew 下载并安装依赖项: brew install carthage 下载成功并初始化脚本后,出现以下错误:原因:需要 npm 来打包响应 js 文件,而机器上未安装该文件。 解决方案:通过 brew 下载并安装 npm(注:brew 真的是个好东西):brew install npm 安装成功后,继续初始化脚本。/Scripts/bootstrap.sh Xcode 相关操作
-
Node 项目通过 .npmrc 文件指定要安装的依赖项的源代码。
-
前端搭建指南:调试本地开发环境的实用技巧 - 在CLI脚手架项目里,别忘了添加库文件及其版本依赖 1. 实时联动与修改: a) 首先确保在当前CLI脚手架项目的`package.json`依赖中加入所用库文件及其正确版本。 b) 通过npm link在本地创建硬连接,使得库与项目能实时修改、同步。运行如下命令: ``` $ cd your-lib-dir $ npm link $ cd your-cli-dir $ npm link your-lib ``` 2. 模拟部署环境: 当需测试线上环境时,解除本地软链接。回到库文件目录并执行: ``` $ cd your-lib-dir $ npm unlink ``` 同样,在CLI项目目录执行: ``` $ cd your-cli-dir $ npm uninstall your-lib --save ``` 注意此时可能会出现因缺少库文件依赖导致的错误,但可暂且忽略,这样能避免将来遗漏或重复安装的问题。 3. 发布后的操作: 如果库已上线,可先清空CLI项目的`node_modules`目录(`rm -rf node_modules`),然后重新安装所有依赖: ``` $ npm install ``` 通过以上步骤,您就能顺畅地调试和部署您的脚手架项目了。 总结 `link` & `unlink` 命令的运用: 1. `npm link`:在本地创建硬连接,便于实时协作与同步。 2. `npm unlink`:模拟线上环境并解除软链接,为正式部署做准备。