node.js 有没有办法在单个 npmrc 文件中配置多个注册表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32633678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any way to configure multiple registries in a single npmrc file
提问by Abhijit Mazumder
Here is my problem. We have a private NPM registry which only works in VPN. I would like to have a fallback registry https://registry.npmjs.orgso that when I am out of VPN it works seamlessly.
这是我的问题。我们有一个私有的 NPM 注册表,它只能在 VPN 中使用。我想要一个后备注册表https://registry.npmjs.org,这样当我没有 VPN 时它可以无缝工作。
P.S.Currently I am using npmrcwhich does a good job in switching between .npmrc files as a workaround
PS目前我正在使用npmrc作为一种解决方法,它在 .npmrc 文件之间切换方面做得很好
回答by José Alberto Ruiz
You can have multiple registries for scoped packagesin your .npmrcfile. For example:
您可以在文件中为范围包有多个注册表.npmrc。例如:
@polymer:registry=<url register A>
registry=http://localhost:4873/
Packages under @polymerscope will be received from https://registry.npmjs.org, but the rest will be received from your local NPM.
@polymer范围内的包将从https://registry.npmjs.org接收,但其余的将从您的本地 NPM 接收。
回答by Gilberto Alexandre
On version 4.4.1, if you can change package name, use:
在 4.4.1 版本上,如果您可以更改包名称,请使用:
npm config set @myco:registry http://reg.example.com
Where @mycois your package scope.
@myco你的包裹范围在哪里。
You can install package in this way:
您可以通过以下方式安装软件包:
npm install @myco/my-package
For more info: https://docs.npmjs.com/misc/scope
回答by bawa g
Not the best way but If you are using mac or linux even in windows you can set alias for different registries.
不是最好的方法,但如果您在 Windows 中使用 mac 或 linux,您可以为不同的注册表设置别名。
##############NPM ALIASES######################
alias npm-default='npm config set registry https://registry.npmjs.org'
alias npm-sinopia='npm config set registry http://localhost:4873/'
回答by Gregra
For anyone looking also for a solution for authentication, I would add on the scoped packages solution that you can have multiple lines in your .npmrcfile:
对于任何也在寻找身份验证解决方案的人,我会添加范围包解决方案,您可以在.npmrc文件中包含多行:
//internal-npm.example.com:8080/:_authToken=xxxxxxxxxxxxxxx
//registry.npmjs.org/:_authToken=yyyyyyyyyy
Each line represents a different NPM registry
每行代表一个不同的 NPM 注册表
回答by PatS
Since it has been a couple years and it doesn't seem possible to do this (using npm alone), a solution to this problem is to use the Nexus Repository Manager(from Sonatype). Nexus supports multiple repositories, lets you order them, and also proxies/caches to improve speed.
由于已经有几年了,而且似乎不可能做到这一点(单独使用 npm),解决这个问题的方法是使用Nexus Repository Manager(来自 Sonatype)。Nexus 支持多个存储库,让您可以对它们进行排序,还支持代理/缓存以提高速度。
A free version and pro/paid version exist. The feature that supports this is described at: https://help.sonatype.com/repomanager3/node-packaged-modules-and-npm-registries
存在免费版本和专业/付费版本。支持此功能的功能在以下位置进行了描述:https: //help.sonatype.com/repomanager3/node-packaged-modules-and-npm-registries
The relevant information is duplicated below so if/when the above URL/link stops working the information is still here.
相关信息在下面复制,因此如果/当上述 URL/链接停止工作时,信息仍然存在。
A repository group is the recommended way to expose all your npm registries repositories from the repository manager to your users, without needing any further client side configuration. A repository group allows you to expose the aggregated content of multiple proxy and hosted repositories with one URL to npm and other tools.
存储库组是将所有 npm 注册表存储库从存储库管理器公开给用户的推荐方式,而无需任何进一步的客户端配置。存储库组允许您使用一个 URL 向 npm 和其他工具公开多个代理和托管存储库的聚合内容。
It lets you create private npm registries
它允许您创建私有 npm 注册表
A private npm registry can be used to upload your own packages as well as third-party packages.
私有 npm 注册表可用于上传您自己的包以及第三方包。
And
和
To reduce duplicate downloads and improve download speeds for your developers and CI servers, you should proxy the registry hosted at https://registry.npmjs.org. By default npm accesses this registry directly. You can also proxy any other registries you require.
为了减少重复下载并提高开发人员和 CI 服务器的下载速度,您应该代理托管在https://registry.npmjs.org的注册表。默认情况下,npm 直接访问此注册表。您还可以代理您需要的任何其他注册表。
So a quick bulleted list of things you do to get this working is:
因此,您为使其正常工作而做的事情的快速项目符号列表是:
Install Nexus
Create a local/private repo (or point to your private repo on another server)
Create a GROUP that lists your private repo, and the public repo.
Configure your $HOME/.npmrc file to point to the "GROUP" just created.
Publish your private npm packages to the local repo.
Users now can run a one time setup.
安装 Nexus
创建本地/私有仓库(或指向另一台服务器上的私有仓库)
创建一个列出您的私有仓库和公共仓库的 GROUP。
将您的 $HOME/.npmrc 文件配置为指向刚刚创建的“GROUP”。
将您的私有 npm 包发布到本地存储库。
用户现在可以运行一次性设置。
npm config set registry https://nexus/content/groups/GROUP
npm config set registry https://nexus/content/groups/GROUP
- Then users can install both public or private packages via
npm install.npm install my-private-package npm install lodash any-other-public-package
- 然后用户可以通过
npm install.npm install my-private-package npm install lodash any-other-public-package
And both your public and private packages can be installed via a simple npm installcommand. Nexus finds the package searching each repo configured in the group and returns the results. So npm still thinks there is just one registry but behind the curtain there are multiple repos being used.
您的公共包和私有包都可以通过一个简单的npm install命令安装。Nexus 查找包搜索组中配置的每个 repo 并返回结果。所以 npm 仍然认为只有一个注册表,但幕后有多个 repos 正在使用。
IMPORTANT NOTE: When you publish your components, you'll need to specify the npm publish --registry https://nexus/content/repositories/private-repo my-private-packagecommand so your package is published to the correct repo.
重要说明:发布组件时,您需要指定npm publish --registry https://nexus/content/repositories/private-repo my-private-package命令,以便将包发布到正确的存储库。
回答by Fernando Fernandes
You can use multiple repositories syntax for the registryentry in your .npmrcfile:
您可以对文件中的registry条目使用多个存储库语法.npmrc:
registry=http://serverA.url/repository-uri/
//serverB.url/repository-uri/
//serverC.url/repository-uri/:_authToken=00000000-0000-0000-0000-0000000000000
//registry.npmjs.org/
That would make your npm look for packages in different servers.
这将使您的 npm 在不同的服务器中查找包。
回答by Kaus Untwale
Some steps you can try. (its how we do it at my workplace)
您可以尝试一些步骤。(这是我们如何在我的工作场所做到的)
- Create a registry group with two (or more) repository source address. One would be your internal private and the other a proxy to npmjs giving priority to the internal one.
- Make this group your registry in the .npmrc file. This way npm will always try to get it from the internal one, if not found get it from the proxy
- 创建具有两个(或多个)存储库源地址的注册表组。一个是您的内部私有,另一个是 npmjs 的代理,优先考虑内部私有。
- 在 .npmrc 文件中将此组设为您的注册表。这样 npm 将始终尝试从内部获取它,如果未找到,则从代理获取它
Hope that helps.
希望有帮助。
回答by davidcyp
I use Strongloop's cli tools for that; see https://strongloop.com/strongblog/switch-between-configure-public-and-private-npm-registry/for more information
为此,我使用了 Strongloop 的 cli 工具;有关更多信息,请参阅https://strongloop.com/strongblog/switch-between-configure-public-and-private-npm-registry/
Switching between repositories is as easy as : slc registry use <name>
在存储库之间切换非常简单: slc registry use <name>
回答by xymopen
I encounter the same problem when my company set up its own registry, so I heavily rework on proxy-registryinto proxy-multi-registriesto solve this problem. Hope it will also helps you.
我遇到的时候我公司建立了自己的注册表,所以我就大量返工同样的问题,代理注册到代理的多注册表来解决这个问题。希望它也能帮助你。
回答by Vasilescu Andrei
As of 13 April 2020 there is no such functionality unless you are able to use different scopes, but you may use the postinstallscript as a workaround. It is always executed, well, after each npm install:
截至 2020 年 4 月 13 日,除非您能够使用不同的范围,否则将不提供此类功能,但您可以使用安装后脚本作为解决方法。它总是执行,还有,以后每次NPM安装:
Say you have your .npmrc configured to install @foo-org/foo-pack-privatefrom your private github repo, but the @foo-org/foo-pack-publicpublic package is on npm (under the same scope: foo-org).
假设您已将 .npmrc 配置为从您的私有 github 存储库安装@foo-org/foo-pack-private,但@foo-org/foo-pack-public公共包在 npm 上(在同一范围内:foo-组织)。
Your postinstallmight look like this:
您的安装后可能如下所示:
"scripts": {
...
"postinstall": "mv .npmrc .npmrcc && npm i @foo-org/foo-pack --dry-run && mv .npmrcc .npmrc".
}
Don't forget to remove @foo-pack/foo-orgfrom the dependenciesarray to make sure npm installdoes not try and get it from github and to add the --dry-runflag that makes sure package.jsonand package-lock.jsonstay unchanged after npm install.
不要忘记从依赖项数组中删除@foo-pack/foo-org以确保npm install不会尝试从 github 获取它并添加--dry-run标志以确保package.json和package-在npm install之后lock.json保持不变。

