Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack production mode - NODE_ENV undefined

I want to make production build of my app using webpack:

"scripts": {
    "build": "webpack --mode production",
  },

In my webpack.config.js I have this line what I am using in whole config:

const isDevelopment = process.env.NODE_ENV !== 'production';

Usage example:
{
  test: /\.s(a|c)ss$/,
    exclude: /\.module.(s(a|c)ss)$/,
    loader: [
      isDevelopment ? 'style-loader' : MiniCssExtractPlugin.loader,
                    'css-loader',
                    {
                        loader: 'sass-loader',
                        options: {
                            sourceMap: isDevelopment
      }
    }
  ]
}

but process.env.NODE_ENV is always undefined.

I am on Windows 10

Am I doing something wrong?

like image 933
David Bubeník Avatar asked Jul 09 '19 21:07

David Bubeník


People also ask

What does Node_env mean?

NODE_ENV is an environment variable that stands for node environment in express server. The NODE_ENV environment variable specifies the environment in which an application is running (usually, development or production).

Why is process env undefined Nodejs?

env file. It was returning undefined because it did not live inside the server file. It was an oversight.

What is process env node env?

In Node. js, process. env is a global variable that is injected during runtime. It is a view of the state of the system environment variables. When we set an environment variable, it is loaded into process.


2 Answers

Way I found is to use argv.mode variable inside export like so:

module.exports = (env, argv) => {
    const isDevelopment = argv.mode !== 'production';
    return {
      // Config...
    }
}
like image 69
David Bubeník Avatar answered Sep 28 '22 05:09

David Bubeník


You could also manually set it like this "build": "set NODE_ENV=production webpack --mode production" (windows only)

or use cross-env Run scripts that set and use environment variables across platforms

https://www.npmjs.com/package/cross-env

"build": "cross-env NODE_ENV=production

like image 45
Ion Avatar answered Sep 28 '22 03:09

Ion