I use package dotenv to set env variables in .env file. Here is the content of .env:
NODE_ENV=development
PORT=4000
Then, I run nodemon --exec babel-node -- src/index.js to start the project.
How to check value of env variables by command line?
I don't want to use console.log in the JS source code, I want something like so (in terminal):
node
> process.env
But it doesn't show the values I have set in .env file.
Could you give me some resolution?
From your console, type following for mac/ubuntu:
echo $NODE_ENV
and following for windows:
echo %NODE_ENV%
If you want to avoid the use of dotenv as sometimes it creates confusion that where to call the dotenv. So, you can use foreman node module and can start your project by command nf start. It will automatically load .env variables into your project without calling dotenv.
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
I Just defined a function in my .bashrc (and then in my .bash_profile) file. Now I'm trying to run this function, that works fine in terminal, from my package.json with npm
So I get this in my .bashrc file:
function emu(){
~/android/Sdk/emulator/emulator -use-system-libs -avd "$1"
}
and I defined this script in my package.json
"scripts": {
...
"emulator": "emu ReactNative",
...
},
And I get this error in console:
> emu ReactNative
sh: 1: emu: not found
Anyone knows how to fix it?
I'm trying to pass environment variables to npm scripts as arguments
with no success.
export ENVIRONMENT=test.proxy.json
npm run test
I'm trying to do something like this in package.json
npm run test --proxy-config-file $ENVIRONMENT
When you do this:
export ENVIRONMENT=test.proxy.json
npm run test
or this:
ENVIRONMENT=test.proxy.json npm run test
then you will pass the "test.proxy.json" string as a value of the environment variable named ENVIRONMENT.
If you want to pass arguments to npm scripts then you may need to use:
npm run test -- --proxy-config-file $ENVIRONMENT
Keep in mind that if you pass the argument to the npm script, it doesn't necessarily mean that it will be passed to other scripts that this script is executing. With environment variables it's the other way around - by default they should be passed from one script to the other but there is still no guarantee as the caller may decide what environment variables to pass, if any.
But it's hard to tell from your question what is your real problem here - the phrase "with no success" is too general to know what is the problem here.
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.