stephan.peters
stephan.peters

Reputation: 1107

webpack merge definePlugin process.env.NODE_ENV undefined

I am using webpack-merge with different webpack.config files. I am trying to pass the node_env (or any other variabels);

How can I access the process env variable from the webpack.common.js file?

This is the webpack.prd.config.js file:

const merge = require("webpack-merge");
const common = require("./webpack.common.js");
const webpack = require("webpack");

module.exports = merge(common, {
    plugins: [
        new webpack.DefinePlugin({
            "process.env": {
                "NODE_ENV": JSON.stringify("production")
            }
        })
    ]
});

This is the webpack.common.js file:

console.log("Environment: " + process.env.NODE_ENV);

module.exports = {
    entry: {
    ...

The 'process.env.NODE_ENV' variable is always undefined.

Upvotes: 2

Views: 2799

Answers (2)

hamodey
hamodey

Reputation: 91

I just realized that webpack -p does not send the value of process.env.NODE_ENV to webapck.conf.js

I solve this headache by

"scripts": {
    "prod": "set NODE_ENV=production && webpack -p",
  },

Just make sure that when you try to compare them just add space at the end of production

var isProd = process.env.NODE_ENV === "production ";

Upvotes: 5

Quoc-Anh Nguyen
Quoc-Anh Nguyen

Reputation: 5086

DefinePlugin only add a global object to your code (what you send to webpack) not webpack's config.

So if you want to NODE_ENV in your webpack config, you must export it first:

export NODE_ENV=production
webpack ....

or

NODE_ENV=production webpack ...

Upvotes: 2

Related Questions