Skip to content

TypeError: Cannot read property 'forEach' of undefined #490

Closed

Description

@sahariko

Issue description

When running the CLI on a stats file (not all stats files, just some), I'm getting the following error, and the following error only:

Could't analyze webpack bundle:
TypeError: Cannot read property 'forEach' of undefined

It's not a lot to go on, there aren't any helpful hints or file names in which it crashes.

Technical info

System:
    OS: macOS 11.6
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 3.08 GB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 12.22.0 - ~/.nvm/versions/node/v12.22.0/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v12.22.0/bin/yarn
    npm: 6.14.15 - ~/.nvm/versions/node/v12.22.0/bin/npm
  npmPackages:
    terser-webpack-plugin: 5.1.3 => 5.1.3 
    webpack: 5.64.1 => 5.64.1 
    webpack-cli: 4.7.2 => 4.7.2 
    webpack-dev-server: 4.7.3 => 4.7.3 
    webpack-manifest-plugin: 3.1.1 => 3.1.1 

Debug info

Using the CLI - npx webpack-bundle-analyzer@latest stats.json, with the following plugins:

Activity

  1. valscion commented on Jan 24, 2022

    @valscion
    Collaborator

    on a stats file (not all stats files, just some),

    Are you able to post a stats file that causes this issue?

    It's possible that some presence checks are needed where we use .forEach. It's possible that when usage of lodash was made smaller, there now is less protection against some data missing and .forEach still being called on them:

  2. sahariko commented on Jan 24, 2022

    @sahariko
    Author

    I don't think it would be possible, since it's a proprietary codebase 😕

  3. sahariko commented on Jan 24, 2022

    @sahariko
    Author
  4. valscion commented on Jan 24, 2022

    @valscion
    Collaborator

    We can't fix it unless we can make a test case. Otherwise we're likely to break the fix in a future release if we don't have test coverage.

  5. sahariko commented on Jan 24, 2022

    @sahariko
    Author

    I completely understand, but I think that more explicit errors can be very helpful, for example check if bundleStats.assets is defined, and if it isn't print a more helpful error message, maybe with a possible reason as to why it's undefined.

    TypeError: Cannot read property 'forEach' of undefined
    

    Is very cryptic, and doesn't even help me understand if there's an issue with this package, my config, or something else.

  6. valscion commented on Jan 24, 2022

    @valscion
    Collaborator

    That is an error that merely comes because something is undefined and then .forEach is getting called on it. We need to know what is undefined to fix it, or to even add a better error message if we can't fix it.

    Without a stats JSON file, we don't know what is undefined and we can't write tests that would ensure we don't regress any error message feature or a test fix in the future.

  7. ilias-t commented on Jan 31, 2022

    @ilias-t

    Here's a compilation-stats.json file that repros the issue (apologies for it being rather large)
    compilation-stats.json.zip

  8. thecristen commented on Apr 14, 2022

    @thecristen

    hi. here's a stats.json file that produces this error, using latest webpack, webpack-cli, webpack-bundle-analyzer:

    Could't analyze webpack bundle:
    TypeError: Cannot read property 'filter' of undefined
    

    stats.json.zip

  9. tiagoevanp commented on May 31, 2022

    @tiagoevanp

    Probably the problem is with the stats.json file and what is declared on webpack file that will generate json file. here you can find the possible configurations for stats conf. Maybe with a 'normal' config the problem should be gone...

    "The stats option lets you precisely control what bundle information gets displayed."

  10. sebastian-nowak commented on Mar 1, 2023

    @sebastian-nowak

    Any updates on this? This is still an issue in the latest lib version, and it makes it unusable.

  11. valscion commented on Mar 1, 2023

    @valscion
    Collaborator

    If someone is able to create a pull request that reproduces this case with a test case and also fixes it then I'll consider accepting it ☺️

  12. sahariko commented on Mar 1, 2023

    @sahariko
    Author

    @valscion Don't know about a fix, but both @ilias-t and @thecristen provided Zip files with a reproducible error.

  13. valscion commented on Mar 2, 2023

    @valscion
    Collaborator

    Yes, I asked about a reproduction as a test case and creating a pull request that aims to fix this bug.

    I don't have time right now to tackle this and it seems that this isn't affecting too many people right now. The quickest way to get this fixed is to help in fixing it as part of open source ☺️

  14. Ismaestro commented on Sep 26, 2024

    @Ismaestro

    Ok, so as an alternative, use source-map-explorer...

    # package.json scripts section
    
    "bundle-report": "ng build --configuration=production-sourcemaps && source-map-explorer dist/angularappname/browser/**/*.js"
    
    # angular.json
    
    "configurations": {
       ...
       "production-sourcemaps": {
          "sourceMap": true,
          "outputHashing": "all"
       },
       ...
    }
    
  15. hecerinc commented on Jan 6, 2025

    @hecerinc

    FYI, I dug into the code a little bit, and it was analyzer.js:getViewerData() function returning the undefined in this chunk:

    			children[i].assets.forEach((asset) => {
    				asset.isChild = true;
    				bundleStats.assets.push(asset);
    			});

    I added the null coalescing operator and then it failed again. Upon inspection it looks like bundleStats.assets was empty, so I figured it was missing a property in the stats when running webpack --profile --json, so I changed the profiling command to webpack --profile --json --stats verbose, and it started working 🚀

  16. valscion commented on Jan 8, 2025

    @valscion
    Collaborator

    Thanks for the insight @hecerinc! I wonder if there's some place in the README.md here where this --stats verbose would be useful to highlight? If so, feel free to send a PR for said update ☺️

  17. ronakmaheshwari commented on Oct 16, 2026

    @ronakmaheshwari
    Contributor

    @valscion can you close it as its merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions