54

I have a local nexus server, I'd like yarn to look through it before going online, basically prefer-offline

yarn install \
  --prefer-offline \
  --cache-folder C:\folder\yarn-cache \
  --preferred-cache-folder C:\folder\yarn-cache \
  --non-interactive \
  --no-lockfile \
  --registry http://server/repository/npm-group

Yarn fails to find a dependency offline then fails to go online. I don't know if that can be fixed.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Lynob
  • 5,059
  • 15
  • 64
  • 114

7 Answers7

51
  • Create an npm (hosted) repository to use as your private registry. I believe you have already done this.
  • Create an npm (proxy) repository pointing to your external repository of choice (https://registry.yarnpkg.com or https://registry.npmjs.org/).
  • Create an npm (group) with your private registry on first position and the proxy registry on second position.
  • Point yarn to your group repository: yarn config set registry http://nexus.local/repository/npm-group/. If required by your Nexus configuration, authenticate yarn: yarn login.

Also be wary of yarn using any npm config you might have over it's own: https://github.com/yarnpkg/yarn/issues/4862

Update for Yarn v2+:

Yarn v2+ uses a different configuration key for updating the NPM registry, called npmRegistryServer (see their Migration page for details):

yarn config set npmRegistryServer http://nexus.local/repository/npm-group/
matth
  • 6,112
  • 4
  • 37
  • 43
Dawid Sawa
  • 1,984
  • 1
  • 12
  • 19
37

You can set a different registry in yarn by using the following command:

yarn config set registry <url-to-your-registry>

Verify that it has been set by typing the following command:

yarn config get registry

For a one-off change in registry you can use the variable YARN_REGISTRY like this:

YARN_REGISTRY="<url-to-your-registry>" yarn config get registry

or

YARN_REGISTRY="<url-to-your-registry>" yarn publish
Vishal Vasnani
  • 501
  • 4
  • 7
8

You can use .yarnrc file & add registry "<your repo URL>" Refer - yarnrc

Shabbir Essaji
  • 584
  • 1
  • 10
  • 17
3

Heads up!

If you define your publishConfig.registry in package.json:

  "publishConfig": {
    "registry": "https://registry.npmjs.org"
  },

It will still override registry, even if --registry param was given to yarn publish!

cadavre
  • 1,334
  • 1
  • 18
  • 34
3

Also don't forget to regenerate yarn.lock file after changing registry. It's neccessary because yarn.lock contains links to old registry and will try to install dependencies from that links.

Alex Belov
  • 31
  • 3
3

Hey Yarn 1 users

  1. If you want to config a registry for a specific repo, it should go to the .npmrc file in your repo. .yarnrc is supposed to configure additional Yarn features

  2. However It's OK if you put it in .yarnrc file. Yarn will read both .npmrc and .yarnrc file.

  3. If you want to config registry globally, run the following command(eg: I am using https://registry.npmmirror.com registry):

    yarn config set registry https://registry.npmmirror.com
    

    Note this add registry to the .yarnrc your user home directory (~/.yarnrc). The added line is like this:

    registry "https://registry.npmmirror.com"
    

For Yarn 2 users, try this answer.

For NPM users

  1. Config registry for a repo:

    npm config --location=project set registry https://registry.npmmirror.com
    
  2. Config registry globally(~/.npmrc):

    npm config --location=global set registry https://registry.npmmirror.com
    
shrekuu
  • 1,716
  • 1
  • 23
  • 38
1

In the case of yarn 3 a.k.a. Berry:

yarn config set 'npmRegistries["http://npm.example.com"]'
kn_pavan
  • 1,510
  • 3
  • 21
  • 42