12

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
2
  • 1
    Have you tried ENVIRONMENT=test npm run test with in your package.json *script* $ENVIRONMENT ?
    – EnbyCherry
    Commented Mar 28, 2017 at 12:11
  • Actually, I tried exactly how @heldt did it, and it worked on my system (i.e. export first, then doing npm run). Not sure why it's not working for him. What OS and npm versions are you on?
    – Paul
    Commented Mar 28, 2017 at 12:25

1 Answer 1

12

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.