Search code examples
javascriptreactjsleafletreact-leaflet

react leaflet Module parse failed: Unexpected token (10:39)


Failed to compile.

./node_modules/@react-leaflet/core/lib/path.js 10:39
Module parse failed: Unexpected token (10:39) You may need an appropriate loader to handle this file type. | useEffect(function updatePathOptions() { | if (props.pathOptions !== optionsRef.current) {

  var options = props.pathOptions ?? {};

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


Solution

  • I had the same problem. After some research, I found the below answers, which helped me fix them.

    1. First, check if your react version matches "react-leaflet" version.
      • For example in "react-leaflet" version 4.x, React v18 and Leaflet v1.8 are now required as peer dependency. ref.
    2. Update browserslist in package.json to this:
    ...    
        "browserslist": [
           ">0.2%",
          "not dead",
          "not op_mini all"
        ],
    ...
    
    1. Delete .cache directory in node_modules and start over.