npm Community Forum (Archive)

The npm community forum has been discontinued.

To discuss usage of npm, visit the GitHub Support Community.

.npmrc does not recognize environment variables

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.:

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


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?

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


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:


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! Invalid tag name "@fortawesome:x": Tags may not have any characters that encodeURIComponent encodes.

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.