Skip to content

[BUG] Cannot read property 'add' of undefined #556

Closed

Description

@louisgv

Here's my .babelrc:

babelrc
{
  "presets": [
    "es2015", "stage-2", "stage-0", "react", "es2015-ie"
  ],
  "plugins": [
    "transform-runtime", "transform-decorators-legacy", "transform-class-properties"
  ],
  "compact": true,
  "env": {
    "test": {
      "plugins": ["istanbul"]
    },
    "production": {
      "presets": [
         "babili"
      ]
    }
  }
}

Running babel resulted in this error:

Details
ERROR in ./src/components/Main/index.js
Module build failed: TypeError: /src/components/Main/index.js: Cannot read property 'add' of undefined
    at ScopeTracker.addReference (/node_modules/babel-plugin-minify-mangle-names/lib/scope-tracker.js:55:36)
    at ReferencedIdentifier (/node_modules/babel-plugin-minify-mangle-names/lib/index.js:190:28)
    at newFn (/node_modules/babel-traverse/lib/visitors.js:318:17)
    at bfsTraverse (/node_modules/babel-plugin-minify-mangle-names/lib/bfs-traverse.js:34:43)
    at Mangler.collect (/node_modules/babel-plugin-minify-mangle-names/lib/index.js:269:9)
    at Mangler.run (/node_modules/babel-plugin-minify-mangle-names/lib/index.js:70:14)
    at PluginPass.exit (/node_modules/babel-plugin-minify-mangle-names/lib/index.js:589:19)
    at newFn (/node_modules/babel-traverse/lib/visitors.js:276:21)
    at NodePath._call (/node_modules/babel-traverse/lib/path/context.js:76:18)
    at NodePath.call (/node_modules/babel-traverse/lib/path/context.js:48:17)
    at NodePath.visit (/node_modules/babel-traverse/lib/path/context.js:117:8)
    at TraversalContext.visitQueue (/node_modules/babel-traverse/lib/context.js:150:16)
    at TraversalContext.visitSingle (/node_modules/babel-traverse/lib/context.js:108:19)
    at TraversalContext.visit (/node_modules/babel-traverse/lib/context.js:192:19)
    at Function.traverse.node (/node_modules/babel-traverse/lib/index.js:114:17)
    at traverse (/node_modules/babel-traverse/lib/index.js:79:12)
    at File.transform (/node_modules/babel-core/lib/transformation/file/index.js:548:35)
    at /node_modules/babel-core/lib/transformation/pipeline.js:50:19
    at File.wrap (/node_modules/babel-core/lib/transformation/file/index.js:564:16)
    at Pipeline.transform (/node_modules/babel-core/lib/transformation/pipeline.js:47:17)
    at transpile (/node_modules/babel-loader/lib/index.js:46:20)
    at Object.module.exports (/node_modules/babel-loader/lib/index.js:163:20)
 @ ./src/dist.js 1:127-153

Now if I disable Mangling, the minification doesn't seem to have much effect:

Details
npm run dist

> BABEL_ENV=production webpack --env=dist

Hash: b6d52d6cc9478801536d
Version: webpack 1.15.0
Time: 4355ms
               Asset    Size  Chunks             Chunk Names
    dist.js  749 kB       0  [emitted]  main
dist.js.map  923 kB       0  [emitted]  main
    + 340 hidden modules

Dist without production env:

Details
> webpack --env=dist

Hash: 8636992c125a9a4e5f8a
Version: webpack 1.15.0
Time: 3934ms
               Asset    Size  Chunks             Chunk Names
    dist.js  752 kB       0  [emitted]  main
    dist.js.map  925 kB       0  [emitted]  main
    + 340 hidden modules

Activity

  1. added
    bug
    on May 24, 2017
  2. boopathi commented on May 24, 2017

    @boopathi
    Member

    Thanks. Do you have a minimal reproduction code snippet or the input code that creates this error ?

    And since you're using webpack, can you try with babili-webpack-plugin to find if the same error happens with babili webpack plugin.

    There are a few issues when used with es2015

  3. joewilson81 commented on May 26, 2017

    @joewilson81

    I was having the same issue today, but I'm not using webpack.

    It was working before some changes I had made, so I stashed them and re-applied them one by one. The culprit was the snippet below in a function that otherwise had no return value:

    if (!someLocalBooleanVariable && this.state.anotherBooleanVariable) {
          return;
    }

    I can't show the client's code, but I'll try to put together an example later and see if I can reproduce the issue.

  4. changed the title [-][SUPPORT] Cannot read property 'add' of undefined[/-] [+][Bug] Cannot read property 'add' of undefined[/+] on May 29, 2017
  5. changed the title [-][Bug] Cannot read property 'add' of undefined[/-] [+][BUG] Cannot read property 'add' of undefined[/+] on May 29, 2017
  6. craftzdog commented on May 31, 2017

    @craftzdog

    I got same error with following code:

    import { app } from 'electron'
    
    app.session = {
      async start () {
        await app.windowManager.createFullWindow({})
      }
    }

    Making alias of app resolved the error:

    import { app } from 'electron'
    
    const app2 = app
    
    app.session = {
      async start () {
        await app2.windowManager.createFullWindow({})
      }
    }

    Hope it helps.

  7. boopathi commented on May 31, 2017

    @boopathi
    Member

    Is everyone here facing this issue ONLY when using babili and es2015-block-scoping in the same transformation (or) both in babelrc ? (es2015-block-scoping is included in preset-es2015 or preset-env with a browser setting that converts let/const to var.)

    Is anyone getting the same bug when used WITHOUT es2015-block-scoping ?

  8. craftzdog commented on May 31, 2017

    @craftzdog

    @boopathi

    My .babelrc is:

    {
      "presets": [
        "stage-0",
        ["env", {
          "targets": {
            "node": "7.4.0",
            "electron": "1.6.7"
          },
          "useBuiltIns": true
        }]
      ],
      "plugins": [
        "add-module-exports",
        "transform-decorators-legacy"
      ],
      "env": {
        "production": {
          "presets": ["babili"]
        }
      }
    }

    When I removed preset-env, it worked.
    But below config with transform-es2015-block-scoping also worked:

    {
      "presets": [
        "stage-0"
      ],
      "plugins": [
        "transform-es2015-block-scoping",
        "add-module-exports",
        "transform-decorators-legacy"
      ],
      "env": {
        "production": {
          "presets": ["babili"]
        }
      }
    }
  9. OR13 commented on Jun 7, 2017

    @OR13

    I am also experiencing this issue. My .babelrc

    {
      "plugins": ["transform-runtime", "lodash", "transform-decorators-legacy"],
      "presets": ["es2015", "react", "stage-0", "babili"]
    }
    

    I can resolve it by turning mangle off:

    {
      "plugins": [
        "transform-runtime",
        "lodash",
        "transform-decorators-legacy"
      ],
      "presets": [
        "es2015",
        "react",
        "stage-0",
        [
          "babili",
          {
            "evaluate": false,
            "mangle": false
          }
        ]
      ]
    }
    
  10. jfurfaro commented on Jun 8, 2017

    @jfurfaro

    I'm also having the issue, but only when transforming my source files with browserify. The second call to babili in gulp-babel doesn't error. Trying babili only in the browserify transform still errors. If I just disable mangle in the browserify transform, it works.

    My gulp task

    gulp.task('scripts', () => {
    	const b = browserify({
    		entries: 'source/app.js',
    		debug: false,
    		paths: ['source/'],
    		transform: [
    			[
    				'babelify', {
    					presets: ['es2017', 'react', 'babili'], // es6 minify on the way in
    					plugins: ['transform-es2015-modules-commonjs', 'transform-object-rest-spread']
    				}
    			]
    		]
    	});
    
    	return b.bundle().on('error', handleError)
    		.pipe(source('app.js'))
    		.pipe(buffer())
    		.pipe(babel({presets: ['babili']})) // es6 minify the whole bundle on the way out
    		.pipe(gulp.dest('dist/'));
    });
    
  11. added
    with es2015
    on Jun 8, 2017
  12. arichiardi commented on Jun 20, 2017

    @arichiardi
  13. alcuadrado commented on Jul 7, 2017

    @alcuadrado

    I'm having this same issue with this .babelrc:

    {
      "presets": [
        [
          "env",
          {
            "targets": {
              "chrome": 55
            }
          }
        ],
        "react",
        "babili"
      ],
      "plugins": [
        "transform-object-rest-spread"
      ]
    }
    
  14. jrencz commented on Jul 11, 2017

    @jrencz

    I'm having the same issue. In my case it happens when there's a class and at least one method name is a symbol. If I switch from symbol to string (either computed or not) it works fine:

    Fails:

    const someSymbol = Symbol('meant to keep method private-ish. not to be exported or exposed any other way');
    
    export default class Foo {
       [someSymbol]() {}
    }

    Works:

    export default class Foo {
       some() {}
    }

    Using: 0.1.3 (latest currently)

    .babelrc
    {
      "presets": [
        "latest",
        "stage-0",
        "babili",
      ],
      "plugins": [
        "babel-plugin-add-module-exports"
      ]
    }
    
  15. 39 remaining items

  16. devongovett commented on May 20, 2018

    @devongovett

    Just made a PR to work around the issue: #860. I'm not exactly sure why it happens - I'm not using the commonjs modules transform but it still happens for me. This seems to fix the issue though by just adding the missing scope before trying to track a reference in it.

  17. LabhanshAgrawal commented on Jan 13, 2020

    @LabhanshAgrawal

    @devongovett This error is occurring again, on updating to babel core 7.8.0

  18. copyrighthero commented on Jan 28, 2020

    @copyrighthero

    I'm upgrading babel from 7.7.4 to 7.8.3 today and this happened to me too, after changing the configuration for minify from {mangle: true} to {mangle: false} I was able to build without problem; the name mangling is the issue for me too :-/

  19. arantes555 commented on Feb 5, 2020

    @arantes555

    Same thing here. It seems to be happening again with 7.8, and seems it can be worked-around with {mangle: false}

  20. eugenmihailescu commented on Feb 10, 2020

    @eugenmihailescu

    In my case the same error occurred due to some function's parameter default value set to a constant.

    const VALUE = 10;
    
    function myFunction(param = VALUE) {...}

    The workaround was to replace the constant in the function declaration, like this:

    function myFunction(param = 10) {...}

    or

    function myFunction(param) {
      if ("undefined" === typeof param) {
          param = VALUE;
      }
    }
  21. somewhatabstract commented on Feb 17, 2020

    @somewhatabstract

    I am seeing this same error when using rollup-babel-minify.

    TypeError: unknown: Cannot read property 'add' of undefined
        at ScopeTracker.addReference (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/scope-tracker.js:47:34)
        at ReferencedIdentifier (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/index.js:196:26)
        at newFn (/Users/jeffyates/git/checksync/node_modules/@babel/traverse/lib/visitors.js:220:17)
        at bfsTraverse (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/bfs-traverse.js:32:43)
        at Mangler.collect (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/index.js:229:7)
        at Mangler.run (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/index.js:54:12)
        at PluginPass.exit (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/index.js:558:19)
        at newFn (/Users/jeffyates/git/checksync/node_modules/@babel/traverse/lib/visitors.js:179:21)
        at NodePath._call (/Users/jeffyates/git/checksync/node_modules/@babel/traverse/lib/path/context.js:55:20)
        at NodePath.call (/Users/jeffyates/git/checksync/node_modules/@babel/traverse/lib/path/context.js:42:17)
    
  22. ambroselittle commented on May 20, 2020

    @ambroselittle

    This is still a problem today. @eugenmihailescu's workaround worked for me, which suggests that this plugin doesn't handle that correctly, even though it's valid. So it really should be re-opened and fixed (or a new issue).

    Anyhow, I switched to terser, since that works for my sitch, and it handles using var/funcs to set default param values just fine.

  23. cerinoligutom commented on May 27, 2020

    @cerinoligutom

    Encountered this today. @eugenmihailescu's workaround worked for me.

    My scenario:
    I'm using TypeScript and an enum value as the default value for a function parameter.

    Did a bit of testing and found out it's not necessarily due to a constant but rather, as long as it's coming from a variable, it'll fail to transform.

  24. monokee commented on Jun 16, 2020

    @monokee

    It's still an issue. (rollup-babel-minify)

  25. dimaqq commented on Mar 1, 2021

    @dimaqq

    Would it be better to reopen this issue or create new one?

  26. ChenYCL commented on Feb 22, 2022

    @ChenYCL

    same tody

  27. added a commit that references this issue on Sep 9, 2026
    fc6d70e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugwith es2015

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions