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.

Brian Zelip
  • 2,909
  • 4
  • 33
  • 45

2 Answers2

2

Use your env vars without curly braces like:

$fontawesome_pro_token
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}

hoima
  • 71
  • 4