node.js NPM - 无法替换配置中的 env:${NPM_TOKEN}
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52015748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NPM - Failed to replace env in config: ${NPM_TOKEN}
提问by funtik
I am trying to build a react app, but when I execute the command npm -iit gives me the following error:
我正在尝试构建一个 React 应用程序,但是当我执行命令时,npm -i它给了我以下错误:
Error: Failed to replace env in config: ${NPM_TOKEN}
at /usr/local/lib/node_modules/npm/lib/config/core.js:415:13
at String.replace (<anonymous>)
at envReplace (/usr/local/lib/node_modules/npm/lib/config/core.js:411:12)
at parseField (/usr/local/lib/node_modules/npm/lib/config/core.js:389:7)
at /usr/local/lib/node_modules/npm/lib/config/core.js:330:24
at Array.forEach (<anonymous>)
at Conf.add (/usr/local/lib/node_modules/npm/lib/config/core.js:328:23)
at ConfigChain.addString (/usr/local/lib/node_modules/npm/node_modules/config-chain/index.js:244:8)
at Conf.<anonymous> (/usr/local/lib/node_modules/npm/lib/config/core.js:316:10)
at /usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:78:16
/usr/local/lib/node_modules/npm/lib/npm.js:61
throw new Error('npm.load() required')
^
Error: npm.load() required
at Object.get (/usr/local/lib/node_modules/npm/lib/npm.js:61:13)
at process.errorHandler (/usr/local/lib/node_modules/npm/lib/utils/error-handler.js:205:18)
at process.emit (events.js:182:13)
at process._fatalException (internal/bootstrap/node.js:448:27)
I am using MacOS High Sierra. I tried to set the NPM_TOKEN as an environment variable with following command:
我正在使用 MacOS High Sierra。我尝试使用以下命令将 NPM_TOKEN 设置为环境变量:
set -x NPM_TOKEN = xyz
but it doesn't work. What is the problem?
但它不起作用。问题是什么?
采纳答案by EsterlingAccime Youtuber
First Possible Solution:
第一个可能的解决方案:
Simple Solution: rm -f ./.npmrc(Deleting a .npmrc file)
简单的解决方案:(rm -f ./.npmrc删除 .npmrc 文件)
Second Possible Solution:
第二种可能的解决方案:
However if you don't want to delete the file, you can simply remove this line of code in the .npmrcfile.
但是,如果您不想删除该文件,则只需删除该文件中的这行代码即可.npmrc。
Line of Code: //registry.npmjs.org/:_authToken=${NPM_TOKEN}(Remove this code)
代码行:(//registry.npmjs.org/:_authToken=${NPM_TOKEN}删除此代码)
Third Possible Solution
第三种可能的解决方案
Worst case scenario:
最坏的情况:
nano ~/.bash_aliasesornano ~/.bash_profile- add
export NPM_TOKEN="XXXXX-XXXXX-XXXXX-XXXXX" - CTRL + X to exit
- Y to save
nano ~/.bash_aliases或者nano ~/.bash_profile- 添加
export NPM_TOKEN="XXXXX-XXXXX-XXXXX-XXXXX" - CTRL + X 退出
- Y 保存
回答by Jezor
Actually proper solution
其实正确的解决办法
Update your CI deployment configuration:
更新您的 CI 部署配置:
npm config set '//registry.npmjs.org/:_authToken' "${NPM_TOKEN}"
npm publish
Remove this line from the .npmrcfile:
从.npmrc文件中删除这一行:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
Example build config
示例构建配置
You can see this solution used in practice in one of my GitHub repositories: https://github.com/Jezorko/lambda-simulator/blob/master/.travis.yml
您可以在我的 GitHub 存储库之一中看到在实践中使用的此解决方案:https: //github.com/Jezorko/lambda-simulator/blob/master/.travis.yml
The encrypted environment variable is an NPM token.
加密的环境变量是一个 NPM 令牌。
Why the other "solutions" are mere workarounds
为什么其他“解决方案”只是解决方法
I've seen answers here and under this questionthat recommend simply removing the variable setting line or .npmrcfile entirely.
我在这里和这个问题下看到了建议简单地完全删除变量设置行或.npmrc文件的答案。
Thing is, the .npmrcfile might not be ignored by your VCS system and modifying it might lead to accidental pushes to your project's repository. Additionally, the file may contain other important settings.
问题是,.npmrc您的 VCS 系统可能不会忽略该文件,修改它可能会导致意外推送到您的项目存储库。此外,该文件可能包含其他重要设置。
The problem here is that .npmrcdoes not allow defaults when setting up environment variables. For example, if the following syntax was allowed, the issue would be non-existent:
这里的问题是.npmrc在设置环境变量时不允许使用默认值。例如,如果允许使用以下语法,则问题将不存在:
//registry.npmjs.org/:_authToken=${NPM_TOKEN:-undefined}
//registry.npmjs.org/:_authToken=${NPM_TOKEN:-undefined}
回答by Rohman Masyhar
I have an easy solution to this issue. After you set your NPM_TOKEN globally into your environment then replace
我有一个简单的解决方案来解决这个问题。在您将 NPM_TOKEN 全局设置到您的环境中后,然后替换
//registry.npmjs.org/:_authToken={$NPM_TOKEN}
with
和
//registry.npmjs.org/:_authToken=$NPM_TOKEN
It's worked well for me on macOS Catalina.
它在 macOS Catalina 上对我来说效果很好。
回答by dang
If you just set your ~/.profile for the first time (OSX, Ubuntu) and added this line: export NPM_TOKEN="XXXXX-XXXXX-XXXXX-XXXXX". Then you must enter this line to the terminal afterward:
如果你只是第一次设置你的 ~/.profile (OSX, Ubuntu) 并添加了这一行:export NPM_TOKEN="XXXXX-XXXXX-XXXXX-XXXXX"。然后你必须在终端输入这一行:
source ~/.profile
回答by anon58192932
For people on Ubuntu coming from google:
对于来自谷歌的 Ubuntu 用户:
- nano ~/.bash_aliases
- export NPM_TOKEN="PUT_YOUR_TOKEN_HERE"
- CTRL+X to exit
- Y to save
- nano ~/.bash_aliases
- 导出 NPM_TOKEN="PUT_YOUR_TOKEN_HERE"
- CTRL+X 退出
- Y 保存
回答by Jordan Dodson
Running npm installin an IDE (like WebStorm) was my problem. I added the NPM_TOKEN environment variable to .bash_profileand restarted my Terminal, but not my IDE! The IDE did not pick up the changes to the environment until I restarted it as well.
运行npm install在IDE(如WebStorm)是我的问题。我将 NPM_TOKEN 环境变量添加到.bash_profile并重新启动了我的终端,但不是我的 IDE!在我重新启动它之前,IDE 并没有接受对环境的更改。

