2
votes

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.

1
Webpack dev server is for serving static clientside files, not serverside code. Where are you checking for the variable? - Paul
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. - Andrew

1 Answers

4
votes

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