Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

react-leaflet build fails with Module parse failed

ErrorFailed to compile ./node_modules/@react-leaflet/core/esm/path.js

Short answer

Since version 4, react-leaflet and @react-leaflet/core ship modern ES2020 code. An older webpack or Babel setup, such as Create React App 4, cannot parse syntax like optional chaining in node_modules. Either let Babel transpile these packages, upgrade the toolchain, or stay on the react-leaflet release that matches your React.

  • Applies to react-leaflet 4, react-leaflet 5, webpack 5, React 18, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check the react-leaflet and webpack versions. The cause is nearly always an old bundler meeting a package built for ES2020.

    npm ls react-leaflet @react-leaflet/core webpack react-scripts
  2. 2Match what you see, then jump to its fix

    1. 1Unexpected token inside node_modules/@react-leaflet, usually pointing at ?. or ??The bundler cannot parse the package's ES2020 outputFix
    2. 2peer dependency warnings or ERESOLVE during npm install next to the parse errorreact-leaflet major version does not match the React versionFix

What does the error look like?

  • Failed to compile ./node_modules/@react-leaflet/core/esm/path.js
  • Module parse failed: Unexpected token (10:41)

Cause 1 of 2

The bundler cannot parse the package's ES2020 output

1Check

The react-leaflet 4.0.0 changelog says ES2020 became the compilation target. Many setups skip Babel for node_modules, so the newer syntax reaches an old parser.

npm ls webpack

2Fix

Run these packages through babel-loader. The babel-loader README says dependencies may need compiling, either with a pattern using test and not or an exclude function.

npm install -D babel-loader @babel/core @babel/preset-env

// webpack.config.js rule
{
  test: /\.(?:js|mjs|cjs)$/,
  exclude: (file) => /node_modules/.test(file) &&
    !/node_modules[\\/](?:@react-leaflet|react-leaflet)/.test(file),
  use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env'] } }
}

3Verify

Rebuild; the parse error should be gone.

npm run build

Confirmed by github.com

Did this fix it?

Cause 2 of 2

react-leaflet major version does not match the React version

1Check

v4 requires React 18 as a peer dependency and v5 requires React 19, per the changelog.

npm ls react react-dom react-leaflet

2Fix

Pick the release for your React. A React 17 project cannot use 4.x at all, and 3.x is the line before the ES2020 target.

# React 18
npm install react-leaflet@4 leaflet
# React 19
npm install react-leaflet@5 leaflet

3Verify

Install cleanly and rebuild.

npm ls react-leaflet
npm run build

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Read the react-leaflet changelog before bumping a major version; build targets and React peers change there.
  • Install leaflet and react-leaflet together; the docs list React, React DOM and Leaflet as peer dependencies.
  • Run the production build in CI after any dependency bump.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.