NPM - Failed to replace env in config: ${NPM_TOKEN}

前端 未结 6 2017
清酒与你
清酒与你 2020-12-15 02:43

I am trying to build a react app, but when I execute the command npm -i it gives me the following error:

Error: Failed to replace env in config:         


        
相关标签:
6条回答
  • 2020-12-15 02:51

    I have an easy solution to this issue. After you set your NPM_TOKEN globally into your environment then replace

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

    with

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

    It's worked well for me on macOS Catalina.

    0 讨论(0)
  • 2020-12-15 02:54

    Running npm install in an IDE (like WebStorm) was my problem. I added the NPM_TOKEN environment variable to .bash_profile and restarted my Terminal, but not my IDE! The IDE did not pick up the changes to the environment until I restarted it as well.

    0 讨论(0)
  • 2020-12-15 02:55

    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:

    source ~/.profile
    
    0 讨论(0)
  • 2020-12-15 03:05

    For people on Ubuntu coming from google:

    • nano ~/.bash_aliases
    • export NPM_TOKEN="PUT_YOUR_TOKEN_HERE"
    • CTRL+X to exit
    • Y to save
    0 讨论(0)
  • 2020-12-15 03:11

    First Possible Solution:

    Simple Solution: rm -f ./.npmrc (Deleting a .npmrc file)

    Second Possible Solution:

    However if you don't want to delete the file, you can simply remove this line of code in the .npmrc file.

    Line of Code: //registry.npmjs.org/:_authToken=${NPM_TOKEN} (Remove this code)

    Third Possible Solution

    Worst case scenario:

    • nano ~/.bash_aliases or nano ~/.bash_profile
    • add export NPM_TOKEN="XXXXX-XXXXX-XXXXX-XXXXX"
    • CTRL + X to exit
    • Y to save
    0 讨论(0)
  • 2020-12-15 03:18

    Actually proper solution

    Update your CI deployment configuration:

    npm config set '//registry.npmjs.org/:_authToken' "${NPM_TOKEN}"
    npm publish
    

    Remove this line from the .npmrc file:

    //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

    The encrypted environment variable is an NPM token.

    Why the other "solutions" are mere workarounds

    I've seen answers here and under this question that recommend simply removing the variable setting line or .npmrc file entirely.

    Thing is, the .npmrc file 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.

    The problem here is that .npmrc does not allow defaults when setting up environment variables. For example, if the following syntax was allowed, the issue would be non-existent:

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

    0 讨论(0)
提交回复
热议问题