如何为.npmrc使用设置环境变量 [英] How to set env var for .npmrc use

查看:731
本文介绍了如何为.npmrc使用设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中需要一个模块来下载私有的npm软件包.为此,我正在使用.npmrc文件提供下载软件包所需的只读令牌.为了将npm提供的令牌保留在文件之外,我希望将其添加为环境变量,并使其在文件中扩展.例如:

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}

在引用安装之前,我不知道如何将NPM_TOKEN添加到环境中.我尝试使用npm预安装脚本:

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}

我尝试使用echo命令进行测试,以查看preinstall是否在.npmrc变量扩展之前运行,但显然没有.我会收到错误,但看不到我的回声日志.我似乎在这里丢失了一些东西.

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.

我知道将令牌放入package.json不能达到将令牌从.npmrc文件中拉出的目的.我实际上正在使用提供env config服务的服务,该服务将用于运行命令并获取所需的令牌.例如. TOKEN=config_service_value.

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.

推荐答案

您可以将环境变量添加到您的.bashrc或其他启动shell文件中.

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

export NPM_TOKEN=my_token_goes_here_foo_bar

这篇关于如何为.npmrc使用设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆