mapbox / mapbox-gl-geocoder

Geocoder control for mapbox-gl-js using Mapbox Geocoding API
https://mapbox.com/mapbox-gl-js/example/mapbox-gl-geocoder/
ISC License
368 stars 180 forks source link

"Uncaught TypeError: EventEmitter is not a constructor" using vite #441

Closed fc closed 2 years ago

fc commented 3 years ago

Steps:

  1. Instantiate mapbox-gl-geocoder

Reproduce it here: https://replit.com/join/qyvahtyiuq-fc14

Or refer to this code snippet:

import React from 'react';
import MapboxGeocoder from '@mapbox/mapbox-gl-geocoder';
import './App.css';

function App() {
  new MapboxGeocoder({
            accessToken: '',
            mapboxgl: '',
            marker: false,
          });
  return (
    <main>
      React⚛️ + Vite⚡ + Replit🌀
    </main>
  );
}

export default App;

Current:

Uncaught TypeError: EventEmitter is not a constructor
    at new MapboxGeocoder (index.js?b9d5:43)
...

Expected: No error

Seems like this is because I'm using vite as this worked previously. events is also not listed as a dependency.

Temporary workaround: Install events

pnpm install events @types/events
CyrilAnac commented 3 years ago

Same issue, @fc thanks for the workaround

janosh commented 2 years ago

So the problem is that mapbox-gl-geocoder needs to specify events as a dep? But then why would it work outside vite?

mbullington commented 2 years ago

Vite doesn't natively do the Node emulation that other bundlers do. See Parcel docs for these examples: https://parceljs.org/features/node-emulation/

This is still our bug—per say. We should include https://www.npmjs.com/package/events in the dependencies.

Thanks!