Terser: minimization bug (Cannot read property '...' of undefined)

Created on 17 Apr 2019  路  7Comments  路  Source: terser/terser

"Uncaught TypeError: Cannot read property 'x' of undefined" when running "webpack" in production mode

(function(A) {
  (function() {
    doPrint();
  })();

  function doPrint() {
    print(A);
  }
}('Hello World!'));


function print(A) {
  if (!A.x) {
    console.log(A);
  }
}

Webpack configuration:

module.exports = {
  mode: 'production',
  optimization: {
    minimize: true
  }
};

package.json:

{
  "scripts": {
    "start": "webpack-dev-server --config=webpack.config.js"
  },
  "devDependencies": {
    "webpack": "4.30.0",
    "webpack-cli": "3.2.3",
    "webpack-dev-server": "3.1.14"
  }
}

When minification is enabled we get error

main.js:1 Uncaught TypeError: Cannot read property 'x' of undefined
    at main.js:1

image

As you can see this is wrong code because of variable e always undefined

var e;
(e = e).x || console.log(e)

If turn off minification all work as expected

Sandbox repository with the bug - https://github.com/maksimr/playground/tree/webpack-minimize-bug

Most helpful comment

I think I just run into the same issue, if I run npm run production on my local everything works just fine , but when deploying to production I get this error:

94% after seal

 ERROR  Failed to compile with 3 errors                                                                                                             2:12:39 PM

 error

1.bundle.js from Terser
TypeError: Cannot read property 'variables' of undefined
    at /home/forge/staging.jobslake.net/node_modules/laravel-mix/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:1:209748
    at /home/forge/staging.jobslake.net/node_modules/laravel-mix/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:1:211383
    at AST_Call.optimize (/home/forge/staging.jobslake.net/node_modules/laravel-mix/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:1:137200)
    at ri.before (/home/forge/staging.jobslake.net/node_modules/laravel-mix/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:1:136977)

Note: I'm using the same npm and node version in my local and production.

All 7 comments

Actually your example produces correct code outside of webpack (at least w/o toplevel option enabled). I took a look what's webpack produce and tried to find out minimal reproducible code:

function wrapper(module, exports) {
    (function (A) {
        (function () {
            doPrint();
        })();

        function doPrint() {
            print(A);
        }
    }("Hello World!"));

    function print(A) {
        if (!A.x) {
            console.log(A);
        }
    }
}

I've created PR with kinda fix just now: https://github.com/terser-js/terser/pull/339

@L2jLiga Thanks, so the problem was in terser? I saw a similar problem in Uglifyjs

I think this is not only the problem of Terser, because Terser grown up on uglifyjs

did anyone solve this?

Yes, this issue were fixed in https://github.com/terser/terser/pull/339.

If you facing with similar issue feel free to create ticket :)

I think I just run into the same issue, if I run npm run production on my local everything works just fine , but when deploying to production I get this error:

94% after seal

 ERROR  Failed to compile with 3 errors                                                                                                             2:12:39 PM

 error

1.bundle.js from Terser
TypeError: Cannot read property 'variables' of undefined
    at /home/forge/staging.jobslake.net/node_modules/laravel-mix/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:1:209748
    at /home/forge/staging.jobslake.net/node_modules/laravel-mix/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:1:211383
    at AST_Call.optimize (/home/forge/staging.jobslake.net/node_modules/laravel-mix/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:1:137200)
    at ri.before (/home/forge/staging.jobslake.net/node_modules/laravel-mix/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:1:136977)

Note: I'm using the same npm and node version in my local and production.

Was this page helpful?
0 / 5 - 0 ratings