7

When I hardcode my auth token into my .npmrc file, the private module installs as expected.

When I replace the hardcoded token with an environment variable, the private module fails to install.

I've tried multiple ways of writing the variable name, as well as the syntax of the variable in the .npmrc file, due to the following resources:

Example .npmrc files:

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

And, example .env files, both with and without strings around the value:

npm_config_npm_token=ABC123
npm_token=ABC123
NPM_CONFIG_NPM_TOKEN=ABC123
NPM_TOKEN=ABC123

Nothing has worked.

Any clues?

EDIT

I'm using npm v6.9.0.

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

2 Answers2

13

I figured out the error in my assumption, and have come to a solution.

tl;dr - create shell-based persistant environment variables, and use the curly braces variable syntax in a .npmrc file.

The error was assuming npm reads project .env files. Apparently npm does not read .env files located in the a project root.

I wanted npm to read from a .env file so that I could keep all relevant data for a project contained within the project.

Instead, I created a shell-based environment variable that is always available. The following code blocks show how to add environment variables to your (oh-my-zsh) shell, even if you git watch and publish your dot files.

# ~/.oh-my-zsh/custom/env.zsh
# be sure this file is gitignored!

export TOKEN="ABC123"
# ~/.zshrc

source $ZSH/custom/env.zsh
# example .npmrc

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

Thanks to this npm forum answer that nudged me towards this shell-based solution.


ps - because the native OS X man pages file export and source under the builtin category, and don't actually provide any details on how each command works, here are links the man page for each:

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

Assuming you're using macOS, try using the source command in the terminal to refresh the env file.

source .env

I had the same problem and looked and the same posts you did but none worked until I tried that. I got the hint from here. I also used curly brackets around my variable in the .npmrc file unlike suggested in the posts you linked. Good luck!

John Wong
  • 220
  • 2
  • 7
  • 1
    Thanks for your input John. I was able to figure out a solution using persistent shell-based env variables. And like you, I came to realize that stack overflow answer that suggests the npm docs are wrong wrt variable syntax, is actually wrong :) (i.e.: the npm docs are correct). – Brian Zelip Apr 08 '19 at 16:38