Failed to replace env in config using Bash & NPM - node.js

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.
I have the following line set in my ~/.bash_profile:
export NPM_TOKEN="XXXXX-XXXXX-XXXXX-XXXXX"
and then in the /path/to/app/.npmrc I have
//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_TOKEN returns the correct token, so the variable definitely exists.
If I run source ~/.bash_profile the error disappears, and I can install as normal.
Any help appreciated as I'm bashing my head against a wall at this problem!

The fix for me was moving export NPM_TOKEN="XXXXX-XXXXX-XXXXX-XXXXX" before my nvm stuff in .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

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/5882a5d738060c027b830bcc2d58824c5d27942b/.github/workflows/deploy.yml#L26-L27
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}

For Windows 10 users
run set NPM_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxx in your cmd prompt OR set it as a environment variable
edit .npmrc, a pair of % should be used, good to run npm i now.
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
or (if above one doesn't work)
//registry.npmjs.org/:_authToken=%NPM_TOKEN%
Btw, if you need a fallback solution:
//registry.npmjs.org/:_authToken=%NPM_TOKEN% //<-- depends on your need
//your-corp-registry-url/:_authToken=%YOUR_CORP_TOKEN% //<-- depends on your need
#your-corp:registry=https://npm.pkg.github.com/path-of-your-corp
registry=https://registry.npmjs.com //<-- fallback registry

I am also getting the same problem when writing any command related to npm. So I solved by node or nodemon . so when you want to start your server use the node and when you want to install any package just remove this and then install it will work.
//registry.npmjs.org/:_authToken=${NPM_TOKEN:-undefined}
It will work in both react and node.js for me.
Run react also by node by creating own server.js file like in node.js
By this, I successfully deployed my application on Heroku
and in production give your env variable on the server where you are deploying and in your own machine put it in .bash_profile and put it in .gitignore
export NPM_TOKEN="XXXXX-XXXXX-XXXXX-XXXXX"

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

Related

Node.js node:internal/modules/cjs/loader:936 error while trying to use discord.js

I'm trying to make a discord bot with the command prefix "*" but when I try to get the bot online/run it, it gives me this error:
node:internal/modules/cjs/loader:936
node:internal/modules/cjs/loader:936 throw err; ^
Error: Cannot find module 'C:\Users\name\const'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47 { code: 'MODULE_NOT_FOUND', requireStack: [] }
The module I'm using is discord.js and i've installed discord.js on CMD and the VSC terminal but it still comes up with the MODULE NOT FOUND error. I've looked around the internet and I can't find one that fixes the problem. I've tried reinstalling Node.js but it won't stop coming back. Please help, Thanks!
same error with me but I just change file dir cause I run node <filename.js> in the wrong dir so I just change like ---
the first step chacks you are in the right dir or not type: > ls
then if you are in the right dir then type: node <filename.js> if not then change your dir to change dir type: cd <filename>
and then run your first app or file for run type: node <filename.js>
I hope your error is solved!
you need to run it as an administrator so sudo(whatever you tryna run) as long as you are in the right directory
On Windows I solved it like this.
I went to the folder: C:\Users\Documents\AppData\Roaming\npm
And I deleted the file. create-react-app.
I started the project again, and it worked.
root cause, my path was messed up, but I only figured that out once I found where my npm packages where. I had thought they were in ../Programs/Roaming/npm etc but since an update (I think to node) my packages were actually in a Python Lib subdir.
Your Answer might be the same as mine, and I found out all this by using the
npm list -g
Command - which showed the globally installed packages, but also highlighted that this was nowhere to be found in my path
Hope this helps someone!
I had the same error and it was a case of trying to run the file in node from the wrong location.
My solution was to use the terminal and CD into the exact folder the file I was trying to run were and then after that I could run node filename.js.
In my case (this error ocurred at running docker container of next app), it was because I was running docker-compose without the flag --build, but of without copy lock files (package-lock.json and/or yarn.lock).
I updated my Dockerfile:
[...]
COPY yarn.lock .
RUN yarn install --frozen-lockfile
CMD [ "yarn", "dev" ]
And after, run with docker-compose up --build
When I was following a JS tutorial i got this error message too.
The problem was I copied an existing JS file and renamed it but it
through that error when i ran it.
When i created a new JS file, and copied the code in, and ran, it worked.
Before starting your server you need to make sure that the start file is mentioned below
cross-env NODE_ENV=production node index<start file name eg:index for index.js>
It happens when you run npm run start without getting into the project first... So, before starting to compile your project, run this command: cd <project folder name>...
It will stop showing the "Missing Module" Error and smoothly run your desired output in your browser
When was trying to find 'npm' & 'Rdme' path from command prompt to update the Rdme was getting this error Log
This error is caused when file can't be found or its a wrong path, In my case I added the missing file to respective folders- configured & updated in Environment variables (after adding missing files)
For the coming next. For me I'v got the error when I tried to run a package.json script with the npx run rather npm run.
In my case, I used chocolatey to install nodejs and it fixed my issue.
Open an Administrator Command Prompt (right click Command Prompt and select "Run as Administrator"), then run the following command:
choco install -y nodejs-lts
I hope it works for you too :)
The error tells that you've required packages that you haven't installed. So please make sure that you've installed every package that you've required in your bot. Also if you're sure that you've installed it, please check it again, there were namely some problems for some people that some of their packages got uninstalled while updating Discord.js to version 13.
For your problem in the comments ("TypeError [CLIENT_MISSING_INTENTS]"), it is now required in Discord.js version 13 to add your needed intents in the client. Please read this part of the Discord.js guide.
I fixed this issue by changing the opened directory of the terminal. Just go to the terminal and type: cd <folder name> until you reach the folder in which your js file is located. Then save and run again.
npm uninstall webpack-cli -D
npm install webpack-cli#3.3.12 --save-dev
than work fine

How do I fix my npm that seems to be infinitely looping?

I was trying to change my npm and npm-cache folders to d:\\npm-global and d:\\npm-cache
I managed to do a npm config set cache d:\npm-cache
When I tried to do a npm config set prefix d:\npm-global I accidentally put a ' in the folder string as I was hitting enter. I deleted the errant folder after trying a npm i -g gulp and to my horror, npm stopped working altogether, it just sits there with my command prompt rocking up to 50% CPU usage.
Help please! I've tried searching the registry and other places, even uninstalled and re-installed the nodejs msi package on Windows. This reset the D:\Program Files\nodejs\node_modules\npm\npmrc file.
Still no luck. I can't get npm to work again. :(
edit A little digging in and I realized there is a second node server running via Adobe Creative Cloud. I doubt this is the problem though as when I run npm from the cmd.exe prompt, the correct (d:\program files\nodejs) version of the npm server is executed for that process.
edit so only sometimes when I hit Control-C, I'll get this error:
seems like it's trying to find a file that doesn't exist. However, I cleared out the AppData\Roaming\npm folder and the reinstalls reset the default d:\program files\nodejs\....npm\npmrc file. I also made sure that the environment variable NODE_PATH is not set. It wasn't set when it was working. I set it when I was trying to make my changes.
You should be able to modify your config settings manually by modifying ~/.npmrc
#jakemingolla was right so I marked that answer as accepted (thanks Jake), but I also asked this in the NPM forum and did a little test after I got it working. I thought this might help someone: https://npm.community/t/i-killed-my-npm-on-windows-trying-to-move-some-folders-reinstalling-nodejs-isnt-resetting-the-problem/5743/8?u=chi11ax

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

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?
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/5882a5d738060c027b830bcc2d58824c5d27942b/.github/workflows/deploy.yml#L26-L27
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}
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
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.
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
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.
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'
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.
I fixed it by setting NPM_TOKEN=""
In github action, i set the env:
jobs:
build:
runs-on: ubuntu-latest
env:
NPM_TOKEN: ""
# ....
I fixed this via passing a docker build arg as
ARG NPM_TOKEN
ENV NPM_TOKEN=$NPM_TOKEN
ENV NPM_CONFIG_TOKEN=$NPM_TOKEN
My scoped registry line with authToken wasn't being read properly inside my docker container. When running inside the container and invoking npm config list is didn't register.
So setting this variable : NPM_CONFIG_TOKEN and running npm config list registers it under a token key in the config.
https://www.runoob.com/linux/linux-shell-variable.html
replace
'//registry.npmjs.org/:_authToken=${NPM_TOKEN}'
with
'//registry.npmjs.org/:_authToken='${NPM_TOKEN}
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.
For people on Ubuntu coming from google:
nano ~/.bash_aliases
export NPM_TOKEN="PUT_YOUR_TOKEN_HERE"
CTRL+X to exit
Y to save
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
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
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.
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
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
For anyone using npm-run-all running into this issue that:
Doesn't want to remove or modify their .npmrc file (because the syntax is actually ok)
Doesn't want to add the variable in question into their shell profile
Doesn't want to set the variable before every single script as "build": "NPM_TOKEN=... ..."
I found that when using npm-run-all any scripts using run-p or run-s that use other scripts (either directly or indirectly) that use the form npm run <SCRIPT> will parse the .npmrc configuration file, which in turn will cause errors if variables are not set.
To fix this, all you need to do is convert any scripts that are used, directly or indirectly by scripts using run-s or run-p to use run-s
Let's take this as an example:
...
"scripts": {
"dev": "run-s build*",
"build:js": "...",
"build:css": "...",
"predev": "npm run clean",
"clean": "rm -rf ./dist ./build"
}
...
Although this script does not call predev directly, it will be run before dev and this will cause configuration to be read, throwing the error
Instead, change it as follows:
...
"scripts": {
"dev": "run-s build*",
"build:js": "...",
"build:css": "...",
"predev": "run-s clean",
"clean": "rm -rf ./dist ./build"
}
...
where predev now uses run-s instead of npm run
I had the same issue on Macbook pro. Actually the issue is with the naming. Previously i added my token to my env file as below
export GITHUB_TOKEN="xxxx-xxxx-xxxx"
Solved
Solved by changing the name to
export GITHUB_ACCESS_TOKEN="xxxx-xxxx-xxxx"
Conclution
When you define access tokens it is better to name it as **_ACCESS_TOKEN so the npm can identify it correctly.
you can also replace the ${NPM_TOKEN} with your own GitHub generated personal token

node 6.11.3 NODE_EXE not found in IntelliJ

I've updated my project to use node 6.11.3. When I now try to start a npm-script by using the Run-configurations provided from IntelliJ, I always receive the following error:
Error: Failed to replace env in config: ${NODE_EXE}
Important: This error appears only if I start npm from within my project. If I use the windows cmd, the error doesn't appear.
What could possibly have changed between node 6.11.2 and 6.11.3? Because with the prior version, everything worked fine.
A workaround for me is to add the NODE_EXE variable to my run configuration, but In my opinion, that shouldn't be needed, because it worked in 6.11.2 too.
Looks as if you have ${NODE_EXE} variable set in one of your npmrc files (see https://docs.npmjs.com/files/npmrc#files), and it can't be properly expanded for some reason when you run your script in the IDE.
is the issue specific to certain project?
how many npm versions do you have installed? Please check that npm chosen in Node.js Interpreters dialog is the same as you use in cmd shell?
please create an env.js file with console.log(process.env) and try running it via npm ("env" : "node env.js") in both cmd console and WebStorm - what is the result?

Cannot find module 'jasmine-reporters'

My 'npm' folder is in D:\Box folder because I don't have space on C.
I configure Protractor to work by adding in System Environment Variables new environment variable named PROTRACTOR with value: .
Then in System Path I've entered: %PROTRACTOR%\bin
And this works for Protractor perfectly.
Now I want to add jasmine-reporters and I paste in npm\node_modules\jasmine-node\node_modules 'jasmine-reporters' and 'jasmine-growl-reporter' folders.
Then run one test with jasmine report in conf.js and I see error message:
Cannot find module 'jasmine-reporters'.
What I try:
Paste 'jasmine-reporters' and 'jasmine-growl-reporter' folders in npm\node_modules
Add in System Environment Variables NODE_PATH: D:\Box\npm\node_modules
The result is the same.
Someone, please help me with this issue.
If you want to keep your global node_modules on the D:\ drive you need to change prefix in npm config as described in npm docs
Short version (in command prompt as administrator):
npm config set prefix d:\box
To see what is the current path you can issue:
npm config get prefix
Go to C:\Users\username\AppData\Roaming\npm\node_modules
-Remove jasmine-reporters
Go to C:\Users\username\AppData\Roaming
-Remove npm-Cache
Reinstall jasmine-reporter and npm

Resources