NPM - Failed to replace env in config: ${NPM_TOKEN}
node.jsReactjsNpmNode Modulesnode.js Problem Overview
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: ${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:
set -x NPM_TOKEN = xyz
but it doesn't work. What is the problem?
node.js Solutions
Solution 1 - node.js
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
ornano ~/.bash_profile
- add
export NPM_TOKEN="XXXXX-XXXXX-XXXXX-XXXXX"
- CTRL + X to exit
- Y to save
Solution 2 - node.js
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}
Solution 3 - node.js
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.
Solution 4 - node.js
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
Solution 5 - node.js
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.
Solution 6 - node.js
The following worked for me. I had to place
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
AFTER the line where i specify
export NPM_TOKEN='mytoken'
Solution 7 - node.js
Im my case moving the export of the token inside my .zsh
(or .bash_profile
) to the top of the file fixed the problem because it has been initialised too late before.
Solution 8 - node.js
https://www.runoob.com/linux/linux-shell-variable.html replace
'//registry.npmjs.org/:_authToken=${NPM_TOKEN}'
with
'//registry.npmjs.org/:_authToken='${NPM_TOKEN}
Solution 9 - node.js
I got this issue while trying to setup a CI/CD job in Gitlab. I eventually found out that the error was caused because the variable that was throwing the error was set to a protected variable.
I changed it under Settings > CI / CD > Variables.
Solution 10 - node.js
For mac
vim ~/.bash_profile
add export NPM_TOKEN=XXXXX-XXXXX-XXXXX-XXXXX
source ~/.bash_profile
also, add the below entry in the .zshrc file to apply the profile when a new terminal tab/window is opened.
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile
fi
Solution 11 - node.js
For people on Ubuntu coming from google:
- nano ~/.bash_aliases
- export NPM_TOKEN="PUT_YOUR_TOKEN_HERE"
- CTRL+X to exit
- Y to save
Solution 12 - node.js
I am also getting this problem but I find a solution when I am pushing my repo on Heroku so I notice that Heroku run the command react-script start or build
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
so this syntax didn't give the error but when I use the same syntax in my system and run the command it gives me. Because usually when we run in our system we use cmd npm or yarn but if you use react-script then it will not gives an error
Solution 13 - node.js
On Windows while using git bash, setting a regular Windows environment variable worked for me. This answer helped setting an environment variable in Git Bash
Solution 14 - node.js
In case of windows and visual studio code - just restart your visual studio, it helps.
Also, how to set this environment variable on windows?
open Registry Editor, and follow \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
, and create there another one "string value" with your token or whatever you need.
Solution 15 - node.js
I fixed it by setting NPM_TOKEN=""
In github action, i set the env:
jobs:
build:
runs-on: ubuntu-latest
env:
NPM_TOKEN: ""
# ....
Solution 16 - node.js
Using AWS CODEARTIFACT
If you use docker, you need to add this to your Dockerfile
...
ARG CODEARTIFACT_AUTH_TOKEN
...
RUN export CODEARTIFACT_AUTH_TOKEN=$CODEARTIFACT_AUTH_TOKEN
RUN npm i
...
This is the .npmrc file
registry=https://sidanmor-codeartifact-main-112233.d.codeartifact.eu-west-1.amazonaws.com/npm/js-utils/
//https://sidanmor-codeartifact-main-112233.d.codeartifact.eu-west-1.amazonaws.com/npm/js-utils/:always-auth=true
//https://sidanmor-codeartifact-main-112233.d.codeartifact.eu-west-1.amazonaws.com/npm/js-utils/:_authToken=${CODEARTIFACT_AUTH_TOKEN}
registry=http://registry.npmjs.org
And the build command will be:
docker build --build-arg CODEARTIFACT_AUTH_TOKEN=xxxyyyzzz . --tag my-tag
Solution 17 - node.js
you can also replace the ${NPM_TOKEN}
with your own GitHub generated personal token
Solution 18 - node.js
In my case I just add export NPM_TOKEN to ~/.bashrc export NPM_TOKEN=60______-69__-44__-be__-2f__________ This is for bash Ubuntu 20.04