Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass shell environment variable as argument in npm script

Tags:

node.js

npm

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
like image 298
heldt Avatar asked Mar 28 '17 12:03

heldt


Video Answer


1 Answers

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.

like image 176
rsp Avatar answered Oct 13 '22 02:10

rsp