2

In my node application, I'm trying to distinguish between my development version of the application and the production version. When I run webpack-dev-server, I use the following command:

NODE_ENV=development webpack-dev-server --inline --hot --content-base ./public

In my application, I try to read what is in NODE_ENV using process.env.NODE_ENV, but it seems to be null. Is this not the correct way to set environment variables when using webpack-dev-server?

Edit: This is in my React application. I use Express to serve the application in production, but I just use webpack-dev-server for development purposes.

2
  • Webpack dev server is for serving static clientside files, not serverside code. Where are you checking for the variable? Commented Feb 7, 2017 at 5:06
  • Oh sorry I should clarify I'm using web-pack-dev-server for my React application. In my React application I would like to have an if statement to see if I'm in development or production. I would use two different urls when making an api call (localhost vs production link) depending on whether I'm in development or production. Commented Feb 7, 2017 at 14:53

1 Answer 1

4

Setting the environment variable NODE_ENV which is accessible through process.env.NODE_ENV at runtime, is specific to Node.js and is not available in your client-side code. In order to make it available in your client-side code you can use webpack's DefinePlugin:

plugins: [
  new webpack.DefinePlugin({
    'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
  })
]

More information about setting environment variables: https://webpack.js.org/guides/production-build/#node-environment-variable

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

Comments

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.