Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set env var for .npmrc use

I need a module in my project to download a private npm package. To accomplish this, I am using a .npmrc file to supply a read-only token needed to download the package. To keep the token supplied by npm out of the file, I wish to add it as an environment variable and let it expand in the file. E.g:

# .npmrc
//registry.npmjs.org/:_authToken=${NPM_TOKEN}

I can't figure out how to get that NPM_TOKEN added to the env before it is referenced for the install. I tried using an npm preinstall script:

"preinstall": "NPM_READ_ONLY_TOKEN=my_token_goes_here_foo_bar"**

But I still get the same error:

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

I tried testing with an echo command to see if preinstall runs before the .npmrc variable expansion, but it apparently does not. I would get the error and not see my echo log. I seem to be missing something here.

I'm aware that putting my token in package.json defeats the purpose of pulling the token out of the .npmrc file. I'm actually using a service that provides env config services that I would use to run a command and get the needed token. E.g. TOKEN=config_service_value.

like image 638
skwny Avatar asked Feb 11 '18 05:02

skwny


People also ask

How do I check my Npmrc settings?

Run npm config ls -l to see a set of configuration parameters that are internal to npm, and are defaults if nothing else is specified.

What is the use of .npmrc file?

The npmrc manages the npm config files. The config setting for npm is gotten from the command line, environment variables and the npmrc files. You can use the npm config command to update and edit the contents of the user and global npmrc files.


3 Answers

You can add the environment variable to your .bashrc or other startup shell file.

export NPM_TOKEN=my_token_goes_here_foo_bar

like image 152
jonathanhculver Avatar answered Oct 04 '22 06:10

jonathanhculver


Here is how one would set an environment variable in Powershell (Windows 10):

$env:ENV_VARIABLE = 'Value of my environment variable'

And here is the reference link for further study

like image 39
Anindya Dey Avatar answered Oct 04 '22 07:10

Anindya Dey


If you are using zsh for your terminal. You should put the environment variable in the .zshenv file.

echo "export NPM_TOKEN=token_goes_here" >> ~/.zshenv

Then you have to restart your terminal and then try echo $NPM_TOKEN, you should be seeing the value of the environment variable.

like image 45
samuellawrentz Avatar answered Oct 04 '22 07:10

samuellawrentz