Skip to content

Fixing "Module Parse Failed" Error After npm Update in React Apps

Problem: After running npm update in a React app (built with create-react-app), you encounter this build failure:

ERROR in ./node_modules/pako/lib/zlib/trees.js 257:106
Module parse failed: Unexpected token
You may need an appropriate loader to handle this file type

This occurs when Babel fails to transpile a dependency containing modern JavaScript syntax (e.g., optional chaining). The error typically appears after updating to a broken Babel version (7.20.0-7.20.2).


Solution: Resolve Babel Version Conflict

The root cause is a Babel bug patched in v7.20.3+. Choose one approach:

Force working Babel versions by adding to package.json:

json
{
  // Add this section to existing package.json
  "overrides": {
    "@babel/core": "7.20.7",
    "babel-loader": "8.3.0"
  }
}

Then execute:

bash
rm -rf node_modules package-lock.json  # Clear existing installs
npm install                           # Reinstall with overrides

⬅️ Method 2: Downgrade Specific Packages

If overrides aren't feasible, pin older versions in dependencies:

json
{
  "dependencies": {
    // ...your existing dependencies,
    "@babel/core": "7.19.6",
    "@babel/generator": "7.19.6",
    "babel-loader": "8.2.5"
  }
}

Reinstall after modifying.


Alternative Fixes by Environment

For Yarn Users

Use resolutions instead of overrides:

json
{
  "resolutions": {
    "@babel/core": "7.20.7"
  }
}

For Next.js Projects

Try adding use server at the top of files using firebase-admin/remote-config.


Explanation

  1. Cause:

    • Babel versions 7.20.0-7.20.2 contain a regression (issue #15132) failing to transpile syntax like param?.()
    • npm update pulls incompatible Babel versions
  2. Why Overrides/Resolutions Work:

    • Forces npm/yarn to ignore the root module's Babel version requirements
    • Ensures dependencies use known-good versions

Avoid Broken Workarounds

Don't modify build configurations (e.g., vue.config.js) to transpile all node_modules:

  • ❌ Adds significant build overhead
  • ❌ Hides underlying compatibility issues

Prevention Tip

Enable selective dependency resolution and use lockfiles (package-lock.json/yarn.lock) to avoid unexpected dependency updates.


Updated Babel versions (≥7.20.3) include the fix. Prefer overrides when possible as it:

  • Minimizes dependency locks
  • Works with create-react-app's hidden configuration
  • Avoids unsafe build modifications

Run npm list @babel/core post-fix to verify the resolved version is ≥7.20.3 or 19.x.