简体   繁体   English

如何在 Yarn 中使用多个 npm 注册表?

[英]How do I use multiple npm registries in Yarn?

I'm trying to setup Yarn 0.17.9 in our environment but I have a problem with our registry.我正在尝试在我们的环境中设置 Yarn 0.17.9,但我的注册表有问题。 We are currently using two registries, official npmjs and our own registry on internal.network (Sinopia).我们目前正在使用两个注册中心,官方 npmjs 和我们自己在 internal.network (Sinopia) 上的注册中心。

The problem is that we are using internal tool to pull packages from one or the other with --registry flag via npm install .问题是我们正在使用内部工具通过 --registry 标志通过npm install从一个或另一个中提取软件包。 yarn add doesn't have --registry option and I'm unable to set the custom registry globally with fallback to npmjs. yarn add没有 --registry 选项,我无法通过回退到 npmjs 全局设置自定义注册表。 I tried .npmrc but it sets only one registry for npm/yarn in project.我试过.npmrc但它只为项目中的 npm/yarn 设置了一个注册表。 .yarnrc doesn't seem to work and is silenced by .npmrc .yarnrc似乎不起作用,并被.npmrc

Is there any way to specify two registries (fallback to second registry if the package is not found in the first one)?有没有办法指定两个注册表(如果在第一个注册表中找不到 package,则回退到第二个注册表)? Or specify different npm registry per package?或者为每个 package 指定不同的 npm 注册表?

For anyone finding this in 2021, yarn now can work with scopes , private registries and auth .对于任何在 2021 年发现这一点的人来说,yarn 现在可以与scopes 、private registries 和 auth 一起使用

For example, I have published private packages such as @my-company-private-scope/my-package to a Verdaccio (fork of Sinopia ) server and my .npmrc configuration is like:例如,我已经将@my-company-private-scope/my-package等私有包发布到VerdaccioSinopia的分支)服务器,我的.npmrc配置如下:

; Lines starting with ; are for .npmrc file comments
; yarn 1.22.10 seems to default registry to https://registry.yarnpkg.com
; no matter what I put here /shrug
; registry=https://registry.npmjs.com/

@my-company-private-scope:registry=https://npm.my-company.com/
//npm.my-company.com/:_authToken=TOKEN

; I did not seem to need this, as
; yarn still installed public and private packages successfully
; based on what ended up in my yarn.lock file
; //npm.my-company.com/:always-auth true

It looks like npm also supports scopes , in that while yarn unpublish doesn't exist, npm unpublish @my-company-private-scope/my-package@1.0.1-5 also worked beautifully.看起来npm 也支持 scopes ,因为虽然yarn unpublish不存在, npm unpublish @my-company-private-scope/my-package@1.0.1-5也工作得很好。

I don't have a need to try multiple scopes on multiple private servers yet (though might ), however I also don't see any good reason it wouldn't work beautifully too.我还不需要在多个私人服务器上尝试多个范围(尽管可能),但是我也没有看到任何好的理由它也不会很好地工作。

Yarn doesn't have support --registry flag as npm but you can set up your registry in .yarnrc Yarn没有支持--registry标志为npm,但您可以在.yarnrc中设置注册表

If your .yarnrc is being ignored it can be copied out of the ~/.yarnrc using: 如果你的.yarnrc被忽略,它可以使用以下方法从〜/ .yarnrc中复制出来:

yarn config set registry http://registry.com/registry//

You can make a .yarnrc file in the root of the project and write this line in that file: 您可以在项目的根目录中创建.yarnrc文件,并在该文件中写入以下行:

registry "https://registry.npmjs.org/"

This will act as a project specific repository. 这将充当项目特定的存储库。

You can try set different registry in .npmrc and .yarnrc . 您可以尝试在.npmrc.yarnrc设置不同的注册表。

So for example: In .npmrc : 例如:在.npmrc

registry=https://yourcompany/
virtual/:_authToken=credentials
always-auth=true

always-auth=true needs to be the very last line in the file always-auth = true 需要是文件中的最后一行

And in .yarnrc : .yarnrc

registry "https://registry.yarnpkg.com"

Then yarn add / npm install + private-pakage/public-packge will work. 然后yarn add / npm install + private-pakage / public-packge将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM