Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't compile react-leaflet #891

Closed
pkdhital opened this issue Jun 6, 2021 · 44 comments
Closed

Can't compile react-leaflet #891

pkdhital opened this issue Jun 6, 2021 · 44 comments

Comments

@pkdhital
Copy link

pkdhital commented Jun 6, 2021

Bug report

Can't compile blank react app after adding react-leaflet. Throws following error:
`

./node_modules/@react-leaflet/core/esm/path.js 10:41
Module parse failed: Unexpected token (10:41)
File was processed with these loaders:

  • ./node_modules/react-scripts/node_modules/babel-loader/lib/index.js
    You may need an additional loader to handle the result of these loaders.
    | useEffect(function updatePathOptions() {
    | if (props.pathOptions !== optionsRef.current) {
    const options = props.pathOptions ?? {};
    | element.instance.setStyle(options);
    | optionsRef.current = options;

`

Expected behavior

Should compile and be able to use react leaflet

Steps to reproduce

npx create-react-app test
cd test
npm install leaflet react-leaflet
add import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet' on src/App.js
npm start

Edit

For me #891 (comment) this resolved issue, but comments below indicate it's not working for everybody. Please try this and check if it's working for you or not.

@MGraefe
Copy link

MGraefe commented Jun 7, 2021

Having the same issue, related thread in create-react-app:

facebook/create-react-app#9468 (comment)

Picked up a lot of activity in the last ~20 days. See last comments for remarks from react guys.

@vyemialyanchyk
Copy link

vyemialyanchyk commented Jun 11, 2021

`Failed to compile.

./node_modules/@react-leaflet/core/esm/path.js 10:41
Module parse failed: Unexpected token (10:41)
File was processed with these loaders:

  • ./node_modules/babel-loader/lib/index.js
    You may need an additional loader to handle the result of these loaders.
    | useEffect(function updatePathOptions() {
    | if (props.pathOptions !== optionsRef.current) {
  const options = props.pathOptions ?? {};

| element.instance.setStyle(options);
| optionsRef.current = options;
`

the same issue

@vyemialyanchyk
Copy link

`Failed to compile.

./node_modules/@react-leaflet/core/esm/pane.js 2:27
Module parse failed: Unexpected token (2:27)
File was processed with these loaders:

  • ./node_modules/babel-loader/lib/index.js
    You may need an additional loader to handle the result of these loaders.
    | export function withPane(props, context) {

const pane = props.pane ?? context.pane;
| return pane ? { ...props,
| pane`

@vyemialyanchyk
Copy link

`Failed to compile.

./node_modules/@react-leaflet/core/esm/layer.js 8:46
Module parse failed: Unexpected token (8:46)
File was processed with these loaders:

  • ./node_modules/babel-loader/lib/index.js
    You may need an additional loader to handle the result of these loaders.
    | export function useLayerLifecycle(element, context) {
    | useEffect(function addLayer() {
const container = context.layerContainer ?? context.map;

| container.addLayer(element.instance);
| return function removeLayer() {

`

@vyemialyanchyk
Copy link

`Failed to compile.

./node_modules/react-leaflet/esm/Pane.js 25:37
Module parse failed: Unexpected token (25:37)
File was processed with these loaders:

  • ./node_modules/babel-loader/lib/index.js
    You may need an additional loader to handle the result of these loaders.
    | }
    |

const parentPaneName = props.pane ?? context.pane;
| const parentPane = parentPaneName ? context.map.getPane(parentPaneName) : undefined;
| const element = context.map.createPane(name, parentPane);

`

@vyemialyanchyk
Copy link

`Failed to compile.

./node_modules/react-leaflet/esm/MapContainer.js 72:30
Module parse failed: Unexpected token (72:30)
File was processed with these loaders:

  • ./node_modules/babel-loader/lib/index.js
    You may need an additional loader to handle the result of these loaders.
    | const contents = context ? /#PURE/React.createElement(LeafletProvider, {
    | value: context

}, children) : placeholder ?? null;
| return /#PURE/React.createElement("div", _extends({}, props, {
| ref: mapRef

`

@vyemialyanchyk
Copy link

I assume this is can be treated as critical..

@lukasa1993
Copy link

how this is not fixed asap ?

@tanmaster
Copy link

Same issue...

@miha42-github
Copy link

Exactly the same problem. This seems to be a problem that keeps on giving in some form or fashion without a consistent resolution.

@luaneko
Copy link

luaneko commented Jun 13, 2021

This issue can be reproduced with next.js as well.

info  - Checking validity of types...
info  - Creating an optimized production build...
info  - Using external babel configuration from /app/babel.config.js
Failed to compile.

./node_modules/@react-leaflet/core/esm/path.js
Module parse failed: Unexpected token (10:41)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|   useEffect(function updatePathOptions() {
|     if (props.pathOptions !== optionsRef.current) {
>       const options = props.pathOptions ?? {};
|       element.instance.setStyle(options);
|       optionsRef.current = options;


> Build error occurred
Error: > Build failed because of webpack errors
    at /app/node_modules/next/dist/build/index.js:17:924
    at async Span.traceAsyncFn (/app/node_modules/next/dist/telemetry/trace/trace.js:6:584)
error Command failed with exit code 1.

luaneko pushed a commit to chiyadev/genshin-schedule that referenced this issue Jun 13, 2021
Paul Cammy fucked up when he released react-leaflet@3.2.0. He broke all event handling related to task popup open/close so nothing works anymore. He also broke compilation: PaulLeCam/react-leaflet#891
@pkdhital
Copy link
Author

pkdhital commented Jun 13, 2021

Ok somewhere else in internet, I found following fix:

  1. Go to package.json and find following segment
    "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] }
  2. And replace it with following

"browserslist": [ ">0.2%", "not dead", "not op_mini all" ]
3. Remove packahe-lock.json file
4. Remove node_modules folder
4. Run project again it should compile.

@designbyadrian
Copy link

designbyadrian commented Jun 13, 2021

This doesn't resolve the issue, and also isn't working for everybody.

Create React App, for example, expects a different structure. You'll discover this if you read the referenced issues.

Don't close this ticket!

@pkdhital pkdhital reopened this Jun 13, 2021
@MGraefe
Copy link

MGraefe commented Jun 13, 2021

Changing the browser target is a stupid workaround but not a fix. It's also not necessary for any of the other probably thousands of packages in a typical project.

@ilindaniel
Copy link

ilindaniel commented Jun 13, 2021

This has already been addressed here:
#881 (comment)
#877 (comment)

A quick fix is to downgrade Leaflet. In your package.json file, change these lines:

"react-leaflet": ">=3.1.0 <3.2.0 || ^3.2.1",
"@react-leaflet/core": ">=1.0.0 <1.1.0 || ^1.1.1"

Then remove the node_modules folder and run npm install.

Optional: npm install --save @monsonjeremy/react-leaflet
See: #885 (comment)
Use at your own risk though, as the author will not be further maintaining this package. It is only to hold over until his PR gets merged.

@vyemialyanchyk
Copy link

vyemialyanchyk commented Jun 14, 2021

new "progressive" style '??' should be replaced with old style '?:' in 5 places which I posted above for quickfix.

@dennisat
Copy link

Com' on, it's only one line hope so , why nobody fixing this?

@mattstrain
Copy link

Same issue trying to import vis-data.

Module parse failed: Unexpected token (1437:21)
File was processed with these loaders:
 * ../../node_modules/react-scripts/node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
|       if (id != null) {
|         // a single item
>         return item ?? null;
|       } else {
|         // just return our array

@daniofilho
Copy link

I fixed this upgrading to webpack 5, if it helps somebody.

@HonzaVrkota
Copy link

I've been trying to run it on Next.js, but it didn't work.
Then I've had upgrade my Next.js on v11, which is support Webpack 5 it starts work properly.

@adeelibr
Copy link

adeelibr commented Jun 29, 2021

any update on this issue? using create-react-app. I am happy to make a PR if I can get some guidance 😅

@dennisat
Copy link

No clean solution so far

But this workaround works for me without eject!

  1. Use the react-app-rewired, read the doc how to add it
  2. Create a config-overrides.js at root with this content
const {
  override,
  addExternalBabelPlugins,
} = require("customize-cra");

module.exports = {
  webpack: override(
    ...addExternalBabelPlugins(
      "@babel/plugin-proposal-nullish-coalescing-operator"
    ),
  ),
};

@mscandan
Copy link

mscandan commented Jul 3, 2021

this answer worked for me

@oasis1992
Copy link

"react-leaflet": ">=3.1.0 <3.2.0 || ^3.2.1",
"@react-leaflet/core": ">=1.0.0 <1.1.0 || ^1.1.1"

this solution is the fastest

@designbyadrian
Copy link

@DVGY

Please use the +1 icon (👍) at the top of the issue instead of writing "me too". Repo owners can find popular tickets that way. Comments are made for discussing the issue.

@alteyss
Copy link

alteyss commented Sep 2, 2021

Hi 🙂

In my case I had to use a webpack rule with the following packages as quoted above :

"react-leaflet": ">=3.1.0 <3.2.0 || ^3.2.1",
"@react-leaflet/core": ">=1.0.0 <1.1.0 || ^1.1.1"

config.module.rules.push(
    {
        test: /\.m?js$/,
        include: [/node_modules\/react-leaflet/],
        use: {
            loader: 'babel-loader',
            options: {
                plugins: [
                    '@babel/plugin-proposal-optional-chaining',
                    '@babel/plugin-proposal-nullish-coalescing-operator'
                ]
            }
        }
    }
);

As you can see, I only include react-leaflet that was causing trouble.

Context : PWA Studio (based on React).

@jayarjo
Copy link

jayarjo commented Sep 4, 2021

Fix this please. It's ridiculous.

@limal
Copy link

limal commented Sep 21, 2021

For Next.js 11 do two following things to make the latest version (react-leaflet@3.2.1) work for you:

  1. In next.config.js specifically target the @react-leaflet/core inside the node_modules like below. Otherwise it won't transpile the offending module.
 const withTM = require("next-transpile-modules")([
  "react-leaflet/node_modules/@react-leaflet/core", // specifying just the "react-leaflet" won't work
]);
  1. To resolve the window is not defined compile error, import your Map component dynamically with the SSR turned off:
const DynamicMap = dynamic(() => import("../../src/components/map/Map"), { ssr: false });

const MapPage = () => {
  return (
    <DynamicMap />
  );
};

@danieltkach
Copy link

danieltkach commented Sep 21, 2021

https://stackoverflow.com/questions/68016021/module-parse-failed-additional-babel-loader/68017963#68017963

In order to run it, the react-leaflet library must be downgraded. These are the steps:

  • remove react-leaflet: npm uninstall react-leaflet
  • update your react-scripts version to 3.3.0 or higher: npm install --save react-scripts@latest
  • remove node_module directory (optional)
  • clean cache: npm cache clean --force
  • Install the working versions: npm i react-leaflet@3.1.0 @react-leaflet/core@1.0.2

You can now run the client doing npm start.

@justrealmilk
Copy link

I find it shocking that telling it to target old browsers fixes this.

The issue started occuring for me when I changed

  "browserslist": {
    "production": [
      "last 2 Chrome versions",
      "last 2 Firefox versions",
      "last 2 Safari versions",
      "iOS >= 13",
      "unreleased versions"
    ],
    "development": [
      "last 2 Chrome versions",
      "last 2 Firefox versions",
      "last 10 Safari versions",
      "iOS >= 13",
      "unreleased versions"
    ]
  }

to

  "browserslist": {
    "production": [
      "last 2 Chrome versions",
      "last 2 Firefox versions",
      "last 2 Safari versions",
      "iOS >= 14",
      "unreleased versions"
    ],
    "development": [
      "last 2 Chrome versions",
      "last 2 Firefox versions",
      "last 2 Safari versions",
      "iOS >= 14",
      "unreleased versions"
    ]
  }

@morganney
Copy link

This lib needs a new maintainer that understands the issue.

@ztolley
Copy link

ztolley commented Nov 5, 2021

Why close an issue that hasn’t been fixed?

rod-glover added a commit to pacificclimate/pcic-react-leaflet-components that referenced this issue Nov 6, 2021
Hit this error while working. Only known fix at present
is to downgrade react-leaflet and @react-leaflet/core.
PaulLeCam/react-leaflet#891
@firflant
Copy link

This issue is still blocking may people from using the package. It should not be closed indeed.

@wh1337
Copy link

wh1337 commented May 24, 2022

Still having this issue.

@dennisat
Copy link

Come on... please, respect the open-source reputation.

@DVGY
Copy link

DVGY commented May 25, 2022

Guys i fixed this issue by forking repo and changing babel.js config. I really think, this fix should exist in react leaftlet as it blocks the usage

@dennisat
Copy link

It is not always easy to change the babel, and it is super ugly in 2022 to add something in babel for just a dependency.

@DVGY
Copy link

DVGY commented May 25, 2022

dependenc
@dennisat
#966
DVGY@205437b
This is what I changed to make it work. Again, the user of library should not add this, the maintainer should add this, so that it does not have any problem

@falcondpr
Copy link

I faced the same issue and was able to resolve it by importing the files separately

import { MapContainer } from 'react-leaflet/MapContainer'
import { TileLayer } from 'react-leaflet/TileLayer'
import { useMap } from 'react-leaflet/hooks'

@MiladJoodi
Copy link

same issue now 🥺

@McTom234
Copy link

McTom234 commented Apr 28, 2024

Same issue (4.2.1)

I was able to fix it by using the exact imports. (import {...} from 'react-leaflet/...')

@tayouthae
Copy link

Add “use client” to the top of your file.

"use client"
// my code here

NextJS assumes a component is a Server Component unless you explicitly tell NextJS the component you want is a Client Component.

@xuzhengrong
Copy link

I also encountered this problem and solved it in a clumsy way! Since umijs is used as the development framework, configuring babel plugins is extremely inconvenient, so I gave up. Finally, I found the source code of react-leaflet from the node_modules directory. I copied the js files under the two directories of /lib and /node_modules/@react-leaflet/core/lib into my own code project. Files with the suffix.d.ts don't need to be copied. Then, I modified the copied code, corrected the import path. Finally, I deleted the react-leaflet configuration in package.json and ran npm uninstall. Then it was all right.

@blythechan
Copy link

react-leaflet: "3.2.5", 
@react-leaflet: "1.1.1", 
webpack: "4.47.0"

Out of all the alleged solutions, this answer worked for me. To expand on the answer, I tweaked mine a bit to something akin to this:

{
     test: /\(js|jsx)$/,
     include: [ path.resolve(__dirname, "src"), path.resolve(__dirname, "node_modules/react-leaflet") ],
     use: {
          loader: 'babel-loader',
          presets: [
               "@babel/preset-env",
               "@babel/preset-react"
          ],
          plugins: [
               "@babel/plugin-proposal-nullish-operator", // version 7.24.6; ??
               "@babel/plugin-proposal-optional-chaining" // .?
         ]
     }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests