Yarn: What is the equivalent to npm's NPM_CONFIG_REGISTRY environment variable?

Created on 30 Nov 2016  路  5Comments  路  Source: yarnpkg/yarn

My company's project currently makes use of private node modules stored in gemfury. In order to successfully fetch the private modules, we make use of npm's NPM_CONFIG_REGISTRY environment variable. We're trying to switch over to using yarn (v0.17.9) but it appears that yarn does not recognize NPM_CONFIG_REGISTRY. I went through the yarn source code to try and find some equivalent to NPM_CONFIG_REGISTRY, but, from my initial look, nothing stands out. Does yarn have such an equivalent environment variable?

cat-compatibility cat-feature good first issue help wanted triaged

Most helpful comment

NPM_CONFIG_REGISTRY did not appear to be working for me in my CI build, configuring it to use verdaccio as a caching proxy.

From this comment I tried setting YARN_REGISTRY to the same value and it appears to have worked.

All 5 comments

From the lack of any response, I'm guessing that yarn does not support NPM_CONFIG_REGISTRY. This should be flagged as a compatibility issue. Perhaps a YARN_CONFIG_REGISTRY env variable could be added. In any case, yarn should accept some kind of env var to work with a node module repo proxy.

Yarn's current (lack of?) support for $npm_config_registry is breaking lifecycle hooks that run npm (or another instance of yarn) in at least some cases. Here's a scenario I've encountered:

  1. Yarn reads a custom registry setting from .npmrc, but doesn't update $npm_config_registry to reflect this. Instead, $npm_config_registry is still https://registry.yarnpkg.com.
  2. Lifecycle scripts (in dependencies, for example) run npm (in some cases even npm install, where the registry definitely matters), but npm sees Yarn's custom $npm_config_registry and so doesn't get the value from .npmrc.
  3. The npm command then fails in cases where the registry matters, like if your package is not in the public registry. Even though Yarn found the correct registry, it didn't properly update the environment to inform downstream npm processes.

This can be fixed by copying the registry into .yarnrc, so that $npm_config_registry has the expected value from the beginning, but it would be nice if this wasn't necessary 鈥撀燳arn should just keep the $npm_config_registry setting in sync with the chosen registry.

@exogen this also fails when using npm to start a script that is running yarn, because then $npm_config_registry is set to https://registry.npmjs.org/ but yarn will still try to download stuff from https://registry.yarnpkg.com it just does not authenticate anymore.

Easy repro case:

mkdir test
cd test
yarn add @some/private-package

cat init.sh << EOF
#!/usr/bin/env bash

set -e
set -x

yarn cache clean
rm -r node_modules || true

env npm_config_registry="https://registry.npmjs.org/" yarn
EOF

chmod +x init.sh
./init.sh

That will fail with something similar to the following

yarn install v0.22.0
[1/4] 馃攳  Resolving packages...
[2/4] 馃殮  Fetching packages...
error An unexpected error occurred: "https://registry.yarnpkg.com/@some/private-package/-/private-package-0.3.0.tgz: Request failed \"404 Not Found\"".
info If you think this is a bug, please open a bug report with the information provided in "/Users/despairblue/test/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

This happens for us because we're having a monorepo and have a top level script that goes through all packages and invokes yarn. The top level script is invoked via npm run init or yarn run init, where the former fails and the latter works.

Our workaround for now is (since we can't make sure that every developer uses yarn run now, which also breaks in other cases like https://github.com/lerna/lerna/issues/688), including this line in our init script declare -x npm_config_registry="https://registry.yarnpkg.com".

https://github.com/yarnpkg/yarn/pull/4330 was recently merged and published in Yarn 1.1.0 but unfortunately does not seem to fix this issue.

For us this required adding a .npmrc file with custom registry and token which gets read from ENV to be able to deploy on Heroku with our Gemfury-hosted NPM module. This requires the token to be present in ENV in development which is quite troublesome.

NPM_CONFIG_REGISTRY did not appear to be working for me in my CI build, configuring it to use verdaccio as a caching proxy.

From this comment I tried setting YARN_REGISTRY to the same value and it appears to have worked.

Was this page helpful?
0 / 5 - 0 ratings