.npmrc does not recognize environment variables

(Brian Zelip) #1

I’m not able to get my project .npmrc file to recognize environment variables set at three different levels (project, user, global).

The only way I’m able to install the private module is by hardcoding the api key into he .npmrc file, which is unacceptable.

I’ve tried creating environment variables like 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?

0 Likes

(John Gee) #2

Environment variables are defined in the shell. By convention they are all upper case, like HOME. For example:

export  FORTAWESOME_PRO_TOKEN=ABC123

You are adding a config setting for fontawesome_pro_token rather than an environment variable, and I don’t see a way to do lookup of other config settings in a config file.

I tried this in my .npmrc file:

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

and reproduced your error, and then added an environment variable:

$ npm  install @fortawesome:x
Error: Failed to replace env in config: ${fontawesome_pro_token}
...
$ fontawesome_pro_token=helloworld npm  install @fortawesome:x
npm ERR! code EINVALIDTAGNAME
npm ERR! Invalid tag name "@fortawesome:x": Tags may not have any characters that encodeURIComponent encodes.
1 Like

(Brian Zelip) #3

Thanks very much @shadowspawn, I’ve got the private modules installing now.

I think I understand now, that the four files mentioned by the npm-config docs above only matter in so far as the scope of where npm gets it configuration from, NOT also about where some .env file can be located.

It sounds like .npmrc won’t look for a .env file in a project root. I was hoping to contain all data relating to a project in the project repo, including env vars.

fyi - I wrote all the env vars above in lower case to match how the npm docs write them.

0 Likes

(system) closed #4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

0 Likes