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

Named export 'use' not found in React module when using React Leaflet in Next.js #1149

Open
omer-ye opened this issue Jan 20, 2025 · 1 comment

Comments

@omer-ye
Copy link

omer-ye commented Jan 20, 2025

``When integrating React Leaflet into a Next.js application, the following error occurs during runtime:

SyntaxError: Named export 'use' not found. The requested module 'react' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'react';
const { createContext, use } = pkg;
This error happens when rendering the /map route that uses React Leaflet components. React, expected to provide named exports (e.g., useState, useEffect), is treated as a CommonJS module, causing the application to crash.

This error happens when rendering the /map route that uses React Leaflet components. React, expected to provide named exports (e.g., useState, useEffect), is treated as a CommonJS module, causing the application to crash.

import * as React from 'react';
import DisplayWrapper from '@components/layouts/displayWrapper';
import { ReactNode } from 'react';
import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet';

const MapView = (): JSX.Element => {
  const position: [number, number] = [51.505, -0.09]; // Example coordinates (London)

  return (
    <MapContainer
      center={position}
      zoom={13}
      style={{ height: '100vh', width: '100%' }}
    >
      {/* Add a Tile Layer */}
      <TileLayer
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
      />
      {/* Add a Marker */}
      <Marker position={position}>
        <Popup>A pretty popup for this marker!</Popup>
      </Marker>
    </MapContainer>
  );
};

MapView.getLayout = (page: ReactNode) => (
  <DisplayWrapper>{page}</DisplayWrapper>
);

export default MapView;

@anaislcno
Copy link

Hello !
I had the exact same issue yesterday at work. And for me, the problem was : I reflexively installed the latest version of react-leaflet (so the 5), but it's only compatible with React 19, whereas our project is in React 18.
Hope it helps!

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

2 participants