Randall
Randall

Reputation: 2858

Cannot read property 'push' of undefined UglifyJSPlugin in webpack error

Already 2 days can't solve the problem.I need to install uglifyJsPlugin in my webpack.But webpack throw an error

webpack.config.js:25
        module.exports.plugins.push(

TypeError: Cannot read property 'push' of undefined

Plese help me how i can resolve this issue.?

Here's my webpack.config.js

const NODE_ENV = process.env.NODE_ENV || "development";

module.exports = {
    entry: "./common",
    output: {
        path: __dirname + "/dist",
        filename:"bundle"
    },
    watch:NODE_ENV == "development",

    devtool:NODE_ENV == "development" ? "cheap-inline-module-source-map" : null,

    module: {

        rules: [{
             test: /\.js$/,
             loader:'babel-loader?optional[]=runtime',
             options: { presets: ['es2015'] }
        }]

    }
};

if (NODE_ENV == "production") {
    module.exports.plugins.push(
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false,
                drop_console:true,
                unsafe:true
            }
        })
        )
}

Upvotes: 1

Views: 1738

Answers (1)

olore
olore

Reputation: 4847

It looks like you don't have plugins defined in your config.

Add this and you should be good to go

plugins: [],

You may want to change the exports a bit, like this:

var config = {
    entry: "./common",
    output: {
        path: __dirname + "/dist",
        filename:"bundle"
    },
    watch:NODE_ENV == "development",

    devtool:NODE_ENV == "development" ? "cheap-inline-module-source-map" : null,

    module: {

        rules: [{
             test: /\.js$/,
             loader:'babel-loader?optional[]=runtime',
             options: { presets: ['es2015'] }
        }]

    },
    plugins: []
};

if (NODE_ENV == "production") {
    config.plugins.push(
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false,
                drop_console:true,
                unsafe:true
            }
        })
    );
}

modules.exports = config;

Upvotes: 4

Related Questions