无法使用 Bash 和 NPM 替换配置中的 env

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35483721/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:15:56  来源:igfitidea点击:

Failed to replace env in config using Bash & NPM

node.jsbashherokunpm

提问by Marc Thomas

I'm trying to use a private NPM module in my application, and need to set appropriate NPM access tokens so that third-party tools (Heroku and CI) can access, and install the module.

我正在尝试在我的应用程序中使用私有 NPM 模块,并且需要设置适当的 NPM 访问令牌,以便第三方工具(Heroku 和 CI)可以访问和安装该模块。

I have the following line set in my ~/.bash_profile:

我在我的中设置了以下行~/.bash_profile

export NPM_TOKEN="XXXXX-XXXXX-XXXXX-XXXXX"

and then in the /path/to/app/.npmrcI have

然后在/path/to/app/.npmrc我有

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

However, whenever I open my terminal, I get the following error on startup:

但是,每当我打开终端时,都会在启动时出现以下错误:

Error: Failed to replace env in config: ${NPM_TOKEN}
    at /Users/marcthomas/.nvm/versions/node/v4.2.1/lib/node_modules/npm/lib/config/core.js:429:13
    at String.replace (native)
    at envReplace (/Users/marcthomas/.nvm/versions/node/v4.2.1/lib/node_modules/npm/lib/config/core.js:424:12)
    at parseField (/Users/marcthomas/.nvm/versions/node/v4.2.1/lib/node_modules/npm/lib/config/core.js:400:7)
    at /Users/marcthomas/.nvm/versions/node/v4.2.1/lib/node_modules/npm/lib/config/core.js:338:17
    at Array.forEach (native)
    at Conf.add (/Users/marcthomas/.nvm/versions/node/v4.2.1/lib/node_modules/npm/lib/config/core.js:337:23)
    at ConfigChain.addString (/Users/marcthomas/.nvm/versions/node/v4.2.1/lib/node_modules/npm/node_modules/config-chain/index.js:244:8)
    at Conf.<anonymous> (/Users/marcthomas/.nvm/versions/node/v4.2.1/lib/node_modules/npm/lib/config/core.js:325:10)
    at /Users/marcthomas/.nvm/versions/node/v4.2.1/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:76:16
/Users/marcthomas/.nvm/versions/node/v4.2.1/lib/node_modules/npm/lib/npm.js:29
throw new Error('npm.load() required')
^

Error: npm.load() required
at Object.npm.config.get (/Users/marcthomas/.nvm/versions/node/v4.2.1/lib/node_modules/npm/lib/npm.js:29:11)
at exit (/Users/marcthomas/.nvm/versions/node/v4.2.1/lib/node_modules/npm/lib/utils/error-handler.js:58:40)
at process.errorHandler (/Users/marcthomas/.nvm/versions/node/v4.2.1/lib/node_modules/npm/lib/utils/error-handler.js:385:3)
at emitOne (events.js:77:13)
at process.emit (events.js:169:7)
at process._fatalException (node.js:221:26)
nvm is not compatible with the npm config "prefix" option: currently set to ""
Run `nvm use --delete-prefix v4.2.1 --silent` to unset it.

However, running echo $NPM_TOKENreturns the correct token, so the variable definitely exists.

但是,运行会echo $NPM_TOKEN返回正确的标记,因此该变量肯定存在。

If I run source ~/.bash_profilethe error disappears, and I can install as normal.

如果我运行source ~/.bash_profile错误消失,我可以正常安装。

Any help appreciated as I'm bashing my head against a wall at this problem!

任何帮助表示赞赏,因为我正在解决这个问题!

回答by Paul Nispel

The fix for me was moving export NPM_TOKEN="XXXXX-XXXXX-XXXXX-XXXXX"before my nvm stuff in .bash_profile

对我的修复是export NPM_TOKEN="XXXXX-XXXXX-XXXXX-XXXXX"在我的 nvm 东西之前移动.bash_profile

from

export NVM_DIR=~/.nvm
source ~/.nvm/nvm.sh
export NPM_TOKEN="XXXXX-XXXXX-XXXXX-XXXXX"

to

export NPM_TOKEN="XXXXX-XXXXX-XXXXX-XXXXX"
export NVM_DIR=~/.nvm
source ~/.nvm/nvm.sh

回答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 Ronald Gemao

In your case you have to do this rm -f ./.npmrc. This worked for me.

在您的情况下,您必须这样做rm -f ./.npmrc。这对我有用。

回答by Rahul Lad

Remove below line from your "./.npmrc" file

从“./.npmrc”文件中删除以下行

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

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