1

I'm not able to get my project .npmrc file to recognize any of the environment variables set at three different scopes (project, user, global).

The only way I'm able to install a private module is by hardcoding the api key into the .npmrc file, which is obviously unacceptable since .npmrc is watched by git.

I've tried creating environment variables as the npm-config docs suggest, i.e.:

  • in a project .env file, where both a .npmrc file and a .env file are siblings of package.json, i.e.: fontawesome_pro_token=ABC123
  • in a user config file, i.e.: $ npm set fontawesome_pro_token ABC123
  • in a global config file, i.e.: $ npm set fontawesome_pro_token ABC123 --global

When I reference the env variable in the project .npmrc file, i.e.:

@fortawesome:registry=https://npm.fontawesome.com/
//npm.fontawesome.com/:_authToken=${fontawesome_pro_token}

I get this error:

Error: Failed to replace env in config: ${fontawesome_pro_token}

When I remove the curly braces around the variable name (as this stack overflow answer suggests), I get the following error:

npm ERR! 401 Unauthorized

Any advice on how to config npm to read env variables?


Incidentally -- if deploying private modules to Netlify, Netlify expects the .npmrc file to use the curly braces for env var syntax, see this gist. I can confirm that using the curly brace syntax in a git watched npmrc file, along with setting a build env var in the netlify project admin dashboard, indeed works.

2 Answers 2

1

Change your casing to UPPERCASE when refer to your .env file or handle environment variables

@fortawesome:registry=https://npm.fontawesome.com/ //npm.fontawesome.com/:_authToken=${FONTAWESOME_PRO_TOKEN}

1

Use your env vars without curly braces like:

$fontawesome_pro_token
1
  • This is wrong. Braces are required.
    – BrianEff
    Apr 9 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.