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

EventEmitter is not a constructor at new MapboxGeocoder (Next.JS & TypeScript) #493

Open
fabiansvensson opened this issue Mar 30, 2023 · 2 comments

Comments

@fabiansvensson
Copy link

I have been using MapboxGeocoder and it has worked fine previously. However, I am now getting this error:
EventEmitter is not a constructor at new MapboxGeocoder (webpack-internal:///./node_modules/@mapbox/mapbox-gl-geocoder/lib/index.js:87:24)

I am using these versions of mapbox-gl and mapbox-gl-geocoder:

  • "@mapbox/mapbox-gl-geocoder": "^5.0.1"
  • "@types/mapbox-gl": "^2.7.10"
  • "mapbox-gl": "^2.13.0"
  • "@types/mapbox__mapbox-gl-geocoder": "^4.7.3"

These should be the up-to-date versions as of today (30/03/2023).

My code (simplified slightly):

import mapboxgl from 'mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';
import MapboxGeocoder from '@mapbox/mapbox-gl-geocoder';
import '@mapbox/mapbox-gl-geocoder/dist/mapbox-gl-geocoder.css';

const geocoder = new MapboxGeocoder({
    accessToken: mapboxgl.accessToken,
    marker: false,
    flyTo: false,
    mapboxgl: mapboxgl
});

map.current.addControl(geocoder);

The error occurs directly at the MapboxGeocoder constructor. It seems from the error message that the error might be TypeScript related. I saw that others have had the same issue, but all of them use Vite and Vue 3:
#441
https://stackoverflow.com/questions/69717406/typeerror-eventemitter-is-not-a-constructor-at-new-mapboxgeocoder
mapbox/mapbox-sdk-js#429

I have tried importing Events directly in my Map Component as such:
import EventEmitter from 'events';

I have also tried importing different versions, re-installing mapbox and geocoder, etc., but the issue persists.

Any ideas why this happens and how to solve it?

@Bernard-Murphy
Copy link

I am having the same issue. Has anyone found a solution? The error I am getting is "Cannot set properties of undefined (setting '_eventEmitter')" Here is my code:

`import Map from "react-map-gl";
import Geocoder from '@mapbox/mapbox-gl-geocoder';
import React, { useRef } from 'react';

const mapboxToken = process.env.REACT_APP_MAPBOX_KEY;

export default function MapboxMap({ coordinates, mapMarkers, onGeoCodeResult, showSearchBox, style }){
const mapRef = useRef(null);
const geocoderRef = useRef(null);

return (
        <Map 
            ref={mapRef}
            mapboxAccessToken={mapboxToken}
            style={{
                height: '100%',
                width: '100%',
                ...style
            }}
            initialViewState={{
                zoom: 9,
                longitude: coordinates[0],
                latitude: coordinates[1]                
            }}
            mapStyle="mapbox://styles/mapbox/streets-v11"
        >
            {showSearchBox ?
            <Geocoder
                mapRef={mapRef}
                containerRef={geocoderRef}
                onResult={onGeoCodeResult}
                mapboxApiAccessToken={process.env.MAPBOX_KEY}
            /> : <></>}
            {mapMarkers}
        </Map> : 
        <div
            style={{ 
                backgroundColor: '#e9eafc',
                ...style
            }}
            className="h-100 w-100"
        />}
    </>
)

}`

@starsep
Copy link

starsep commented Dec 23, 2023

I also had this issue while using migrating from Create React App to Vite.

Resolved by npm install events based on #366 (comment)

starsep added a commit to openstreetmap-polska/openaedmap-frontend that referenced this issue Dec 23, 2023
events was needed for mapbox-gl-geocoder
mapbox/mapbox-gl-geocoder#493 (comment)
starsep added a commit to openstreetmap-polska/openaedmap-frontend that referenced this issue Dec 23, 2023
events was needed for mapbox-gl-geocoder
mapbox/mapbox-gl-geocoder#493 (comment)
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

3 participants