Berry: [Feature] Support env variables in yarnrc

Created on 2 Apr 2020  路  5Comments  路  Source: yarnpkg/berry

  • [x] I'd be willing to implement this feature
  • [ ] This feature can already be implemented through a plugin

Describe the user story

yarn v1 supports env variables in yarnrc/npmrc:

//registry.npmjs.org/:_authToken=${COMPANY_NPM_TOKEN}

but v2 doesn't support it in .yarnrc.yml

Describe the solution you'd like

It would be nice to support env variables like in other tools/libraries (for example docker compose)

enhancement

Most helpful comment

All 5 comments

You can inject runtime environment variables, including variables already defined within the active shell, by rerouting the yarnPath defined in .yarnrc.yml to a custom path containing a .js file which instantiates a valid Yarn v2 release in the form of a child_process. At that point, declaring custom process.env values is straight-forward. Though it does feel a bit hacky. Check out @yarkPkg Berry's repo and you can see they do exactly that. However, I imagine best practices would be to utilize the pnpapi lifecycle as according to the docs.

A quick but not that convenient solution, until we add support for setting complex values (Arrays and Objects) through environment variables (e.g. YARN_NPM_REGISTRIES), would be to use yarn config set --json (It's a very new feature, so you have to use yarn set version from sources to get the latest Yarn version from master):

yarn config set --json npmRegistries '{ "//registry.npmjs.org": { "npmAuthToken": "'"${COMPANY_NPM_TOKEN}"'" } }'

Note that this command modifies your .yarnrc.yml file (so make sure you don't commit it) and it also displays the modified value, so make sure to hide its output on CI if your logs are public.

I'm getting around this on CI by setting YARN_NPM_AUTH_TOKEN as an ENV var https://yarnpkg.com/configuration/yarnrc

@njradford yeah, I am doing the same thing right now.

@paul-soporan @arcanis what do you think if we will traverse config here and replace all ${SOME_ENV} by real env from process.env?
https://github.com/yarnpkg/berry/blob/5f0439a087b8463d97fb70c96dc6f983a91be512/packages/yarnpkg-parsers/sources/syml.ts#L127

If you don't mind I can send a PR

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GaoxinHuang picture GaoxinHuang  路  3Comments

joshmeads picture joshmeads  路  4Comments

bradleyayers picture bradleyayers  路  3Comments

wojtekmaj picture wojtekmaj  路  3Comments

chrisands picture chrisands  路  3Comments