0

I have an npm script to start the webpack dev server, but first, my env setup -

in my .zshrc - export DEV_SERVER_PORT=8001

echo returns expected results

>echo $DEV_SERVER_PORT
8001

here's the script i'm running from package.json via npm run

webpack-dev-server --inline --hot --port 8001

this executes correctly via npm run -

echo $DEV_SERVER_PORT

result: 8001

this produces disappointing results via npm run-

 webpack-dev-server --inline --hot --port $DEV_SERVER_PORT

result webpack-dev-server --inline --hot --port $DEV_SERVER_PORT

9
  • Please show what you tried in more detail, including how you exported the desired value to the environment. A MCVE would be ideal (that is, a complete and verifiable reproducer, per stackoverflow.com/help/mcve). Commented May 24, 2016 at 17:13
  • ...btw, as an aside, all-caps names are used by the system and shell; it's lower-case environment variable names (or, more precisely, names containing at least one lower-case character) that are reserved for application use. See the relevant spec at pubs.opengroup.org/onlinepubs/009695399/basedefs/…, fourth paragraph. Commented May 24, 2016 at 17:14
  • Possible duplicate of Passing environment-dependent variables in webpack Commented May 24, 2016 at 17:18
  • ...in general, the answer is somewhere between "yes", and "very yes". From a shell script called via system() from npm, it's just "$varname" as usual; for native JavaScript code it's process.env.varname. But the difference between regular shell variables and environment variables is critical here -- if you didn't export it to the environment, it won't be present. Commented May 24, 2016 at 17:18
  • The main issue is that the webpack cli doesn't seem to want to natively accept env vars as configuration. While many node CLI's do, they don't for whatever reason. See my 'possible duplicate' link to see if that works for you, and if not then update your question. Commented May 24, 2016 at 17:19

2 Answers 2

1

The following result from zsh, reported in your question:

>declare -p DEV_SERVER_PORT
typeset DEV_SERVER_PORT=8000

...means that your shell variable isn't actually exported, and thus isn't available in subprocesses' environment.


Run the following:

export DEV_SERVER_PORT

and, following that, you should see the following:

>declare -p DEV_SERVER_PORT
typeset -x DEV_SERVER_PORT=8000

The -x indicates that the export flag is set, and this variable (with any changes to same) is set in any subprocess invoked.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this, i confused myself. made a change i had forgotten. thanks for pushing through my stubbornness.
0

You need to use process.env. This is an object that contains all the process's environment variables. E.g., if you define an environment variable PORT, it will be accessible using:

process.env.PORT

You may need to convert the value to the correct type. process.env always contains the values as strings, hence - e.g. for a port - you might need to convert the value to a number.

So you end up with something such as:

const port = process.env.PORT - 0;

1 Comment

webpack-dev-server --inline --hot --port process.env.DEV_SERVER_PORT as a script within package.json yields the execution of webpack-dev-server --inline --hot --port process.env.DEV_SERVER_PORT ...not quite right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.