kraklin
kraklin

Reputation: 546

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. We are currently using two registries, official npmjs and our own registry on 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. yarn add doesn't have --registry option and I'm unable to set the custom registry globally with fallback to npmjs. I tried .npmrc but it sets only one registry for npm/yarn in project. .yarnrc doesn't seem to work and is silenced by .npmrc

Is there any way to specify two registries (fallback to second registry if the package is not found in the first one)? Or specify different npm registry per package?

Upvotes: 42

Views: 36411

Answers (5)

rickenbacker
rickenbacker

Reputation: 91

As for version 3 (3.4.1), You can define multiple repositories by putting such config in .yarnrc.yml file (example of GitHub Packages):

npmScopes: 
  your-organization-or-account:
    npmRegistryServer: "https://npm.pkg.github.com"

For more options - Yarn docs reference (search for 'npmScopes'): yarn-docs

Edit:

It also works same way for version 4 (4.0.2 currently).

Upvotes: 4

pzrq
pzrq

Reputation: 1897

For anyone finding this in 2021, yarn now can work with scopes, private registries and 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:

; 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/[email protected] also worked beautifully.

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.

Upvotes: 10

congce wang
congce wang

Reputation: 87

You can try set different registry in .npmrc and .yarnrc.

So for example: In .npmrc:

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

always-auth=true needs to be the very last line in the file

And in .yarnrc:

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

Then yarn add/npm install + private-pakage/public-packge will work.

Upvotes: 5

Himanshu sharma
Himanshu sharma

Reputation: 7891

You can make a .yarnrc file in the root of the project and write this line in that file:

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

This will act as a project specific repository.

Upvotes: 9

Andrés Andrade
Andrés Andrade

Reputation: 2223

Yarn doesn't have support --registry flag as npm but you can set up your registry in .yarnrc

If your .yarnrc is being ignored it can be copied out of the ~/.yarnrc using:

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

Upvotes: 7

Related Questions