Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 585
kraklin Avatar asked Nov 28 '16 13:11

kraklin


People also ask

Does Yarn work with npm registry?

Yarn can consume the same package. json format as npm, and can install any package from the npm registry. This will lay out your node_modules folder using Yarn's resolution algorithm that is compatible with the node.

Where does Yarn look for Yarnrc?

Yarnrc files (named this way because they must be called . yarnrc. yml ) are the one place where you'll be able to configure Yarn's internal settings. While Yarn will automatically find them in the parent directories, they should usually be kept at the root of your project (often your repository).

Which is better Yarn or npm?

As previously stated, Yarn installs dependency packages in parallel, whereas NPM installs them sequentially. As a result, Yarn outperforms NPM when installing bigger files. Both tools can save dependent files to the offline cache.


3 Answers

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.

like image 94
Himanshu sharma Avatar answered Sep 20 '22 08:09

Himanshu sharma


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// 
like image 27
Andrés Andrade Avatar answered Sep 20 '22 08:09

Andrés Andrade


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.

like image 40
pzrq Avatar answered Sep 19 '22 08:09

pzrq