react-leaflet build fails with Module parse failed
Failed to compile ./node_modules/@react-leaflet/core/esm/path.jsShort 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.
Fix it fast
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-scripts2Match what you see, then jump to its fix
What does the error look like?
Failed to compile ./node_modules/@react-leaflet/core/esm/path.jsModule 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 webpack2Fix
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 buildConfirmed by github.com
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-leaflet2Fix
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 leaflet3Verify
Install cleanly and rebuild.
npm ls react-leaflet
npm run buildConfirmed by github.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Leaflet cannot find the map container element
L.map('map') throws on page load
Leaflet looks up the container by id or takes an element you pass.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix