简体   繁体   English

npm 配置文件不读取环境变量

[英]npm config file not reading environment variables

I'm not able to get my project .npmrc file to recognize any of the environment variables set at three different scopes (project, user, global).我无法让我的项目 .npmrc 文件识别在三个不同范围(项目、用户、全局)设置的任何环境变量。

The only way I'm able to install a private module is by hardcoding the api key into the .npmrc file, which is obviously unacceptable since .npmrc is watched by git.我能够安装私有模块的唯一方法是将 api 密钥硬编码到 .npmrc 文件中,这显然是不可接受的,因为 .npmrc 被 git 监视。

I've tried creating environment variables as the npm-config docs suggest, ie:我已经尝试按照npm-config文档的建议创建环境变量,即:

  • in a project .env file, where both a .npmrc file and a .env file are siblings of package.json , ie: fontawesome_pro_token=ABC123在项目.env文件中,其中.npmrc文件和.env文件都是package.json的兄弟,即: fontawesome_pro_token=ABC123
  • in a user config file, ie: $ npm set fontawesome_pro_token ABC123在用户配置文件中,即: $ npm set fontawesome_pro_token ABC123
  • in a global config file, ie: $ npm set fontawesome_pro_token ABC123 --global在全局配置文件中,即: $ npm set fontawesome_pro_token ABC123 --global

When I reference the env variable in the project .npmrc file, ie:当我在项目.npmrc文件中引用环境变量时,即:

@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 on how to config npm to read env variables?关于如何配置 npm 以读取环境变量的任何建议?


Incidentally -- if deploying private modules to Netlify, Netlify expects the .npmrc file to use the curly braces for env var syntax, see this gist .顺便说一句——如果将私有模块部署到 Netlify,Netlify 期望 .npmrc 文件使用花括号来表示 env var 语法,请参阅此要点 I can confirm that using the curly brace syntax in a git watched npmrc file, along with setting a build env var in the netlify project admin dashboard, indeed works.我可以确认在 git 监视的 npmrc 文件中使用花括号语法,以及在 netlify 项目管理仪表板中设置构建环境变量,确实有效。

Change your casing to UPPERCASE when refer to your .env file or handle environment variables引用您的 .env 文件或处理环境变量时,将您的大小写更改为大写

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

使用没有花括号的环境变量,例如:

$fontawesome_pro_token

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM