当前栏目
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.
参考
版权声明
本博客所有的原创文章,作者皆保留版权。转载必须包含本声明,保持本文完整,并以超链接形式注明作者后除和本文原始地址:https://blog.mazey.net/2950.html
(完)
相关文章
- [javascript] 报错SecurityError: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.
- [js]解决postMessage跨域、跨iframe消息传递
- [javascript] 全国省市县JSON/XML数据(包含台湾和海外国家一级)
- [javascript] 解决移动端手机浏览器软键盘遮挡输入框问题
- [PHP]json_encode中文JSON_UNESCAPED_UNICODE在php5.3返回null
- [javascript] 使用javascript实现webrtc视频聊天demo
- [javascript] vue和elementui中的键盘事件
- [javascript] 变量提升
- [前端] js中call方法的理解和思考
- [javascript] ie下audio不支持一些媒体类型
- [javascript] ie下不支持incudes属性和方法
- [javascript] ES6模板字符串
- [javascript] strict模式严格模式
- [javascript]使用babel转换es6语法
- [js] js中的闭包以及特点
- 在Vue中使用JSX,很easy的
- JS的深浅复制,原来如此!
- js小知识
- [javascript] elementui和vue下复制粘贴上传图片
- [javascript] cdn模式下vue和vue-router实现路由