后台运行npm命令时,往往通过命令行指定相关配置,最常用的是使用--registry指定依赖安装源。npminstall--registry=https://registry.npmmirror.com使用.npmrc也可以达到同样的效果:registry=https://registry.npmmirror.com.npmrc用法介绍.npmrc(NPMRunningConfiguration)即可指定npm命令运行时的配置内容,以及可以设置的配置,参见官方文档:config。配置优先级顺序如下:项目内配置文件(/path/to/my/project/.npmrc)用户配置文件(~/.npmrc)全局配置文件($PREFIX/etc/npmrc)NPM内置-在配置文件(/path/to/npm/npmrc)中。在npmrc文件中,以键值对(key=value)的形式设置值:key=value数组:key[]=valuekey[]=value使用#或;发表评论:#Comment1;Comment2key=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-sassnpminstallnode-sass同:sass_binary_site=https://registry.npmmirror.com/-/binary/node-sass案例代码:https://github.com/mazeyqian/mazey/blob/master/.npmrcNPM配置获取当前配置指定值的快捷命令:npmconfiggetsass_binary_site#https://registry.npmmirror.com/-/binary/node-sassnpmconfiggetregistry#https://registry.npmmirror.com设置配置指定值:npmconfigsetexample_key_1example_value_1npmconfiggetexample_key_1#example_value_1显示配置列表:npmconfiglist;userconfigexample_key_1="example_value_1"registry="https://registry.npmjs.org/"注意,如果要发布npm,一定要注意修改registry为你要发布的地址,不要把公司设为private包发布到官方存储库。如果您随后想要发布一个包供全世界查看,您可以简单地覆盖该发布命令的--registry选项。参考淘宝npm域名即将切换&&npmmirror重构升级可以运行自己的私有注册中心吗?解决NPMInstallationofnode-sassfailedduetonetworkproblemstimeout版权声明本博客所有原创文章,作者保留版权。转载必须包含此声明,保持本文完整,并以超链接的形式注明作者及本文原文地址:https://blog.mazey.net/2950.html(完)
