question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'forEach' of undefined if object destructing with rest props is used

See original GitHub issue

Describe the bug

Compilation fails with TypeError: Cannot read property 'forEach' of undefined if object destructing with rest props is used in npx created app. Works with yarn though.

Versions of react-scripts affected: 3.1.0, 3.1.1 (with different error messages), 3.1.2 Most recent react-scripts version that works: 3.0.1

Did you try recovering your dependencies?

npm --version: 6.11.3

Which terms did you search for in User Guide?

TypeError: Cannot read property ‘forEach’ of undefined

Environment

$ npx create-react-app --info

Environment Info:

  System:
    OS: macOS 10.14
    CPU: (8) x64 Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz
  Binaries:
    Node: 12.11.0 - ~/.nvm/versions/node/v12.11.0/bin/node
    Yarn: Not Found
    npm: 6.11.3 - ~/.nvm/versions/node/v12.11.0/bin/npm
  Browsers:
    Chrome: 77.0.3865.90
    Firefox: 67.0
    Safari: 12.0
  npmPackages:
    react: ^16.10.1 => 16.10.1 
    react-dom: ^16.10.1 => 16.10.1 
    react-scripts: 3.1.2 => 3.1.2 
  npmGlobalPackages:
    create-react-app: Not Found

(paste the output of the command here.)

Steps to reproduce

(Write your steps here:)

  1. Create new app using npx: $ npx create-react-app error-test. Make sure yarn is not installed.
  2. Go to error-test/src/App.js and add following code to the end of the file:
const testCase = {
  restObjectPropsFails: ({ id, ...item }) => {
    return null;
  }
}
  1. Run $ npm start from error-test directory

Expected behavior

Application is compiled correctly

Actual behavior

Compilation failed with message:

Failed to compile.

./src/App.js
TypeError: Cannot read property 'forEach' of undefined

Reproducible demo

(Paste the link to an example project and exact instructions to reproduce the issue.)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:71
  • Comments:41 (3 by maintainers)

github_iconTop GitHub Comments

28reactions
iMericacommented, Sep 30, 2019

Downgrading React Scripts to 3.0.1 worked for me.

11reactions
chrisbullcommented, Sep 30, 2019

The issue is definitely eslint-loader. Thanks @alexandrtovmach

Until that merge gets included, adding the following to package.json worked for me

"resolutions": {
    "eslint-loader": "3.0.2"
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read property 'forEach' of undefined in React
It seems to be eslint-loader build error - please take a look at this. Try updating to newest version of eslint-loader and then...
Read more >
for...in - JavaScript | MDN - MDN Web Docs
The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), ...
Read more >
typeerror: cannot read properties of undefined (reading 'map') react ...
If you are going to map through an object: import React, { useEffect } from "react"; const ViewAll = (props) => { const...
Read more >
7 Tips to Handle undefined in JavaScript - Dmitri Pavlutin
JavaScript doesn't initialize variables or object properties with null . ... TypeError: Cannot read property '<prop-name>' of undefined ...
Read more >
Cannot Read Property 'Foreach' Of Undefined - ADocLib
Describe the bug Compilation fails with TypeError: Cannot read property 'forEach' of undefined if object destructing with rest props is used. TypeError: Cannot ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found