Next.js encountered an error with Mapbox-gl-geocoder: TypeError - EventEmitter cannot be used as a constructor

In my ongoing Next.JS version 13 project, I have been successfully using Mapbox-GL and mapbox-gl-geocoder. However, recently I encountered an error when accessing the map that reads:

EventEmitter is not a constructor at new MapboxGeocoder (webpack-internal:///./node_modules/@mapbox/mapbox-gl-geocoder/lib/index.js:87:24)

https://i.sstatic.net/DxOGX.png

The specific snippet of code in question is as follows:

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 issue arises at the construction of MapboxGeocoder with new MapboxGeocoder

I came across similar problems faced by Vue 3 and vite users which are outlined here: TypeError: EventEmitter is not a constructor at new MapboxGeocoder

In an attempt to address this, I tried importing Events alongside MapboxGeocoder directly inside my Map Component:

import EventEmitter from 'events';

However, this approach did not yield favorable results. It should be noted that I am working with up-to-date versions that remained unchanged since the last time this functionality was functioning correctly, approximately a week or two ago:

"@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",

If anyone has any insights or solutions on how to tackle this issue, your input would be greatly appreciated :)

Answer №1

After encountering some issues, I decided to start a new project from scratch and integrate mapbox-gl along with Geocoder. Surprisingly, everything worked seamlessly even though I used the same versions as my original project. It appears that the problem may lie within the dependencies specified in the package.json file or the configuration settings.

Eventually, I was able to identify the root cause of the issue. It turned out that my colleague had made changes to the package.json file by including ./lib in the NODE_PATH variable:

NODE_PATH=./node_modules:./lib next dev -p 8090

It seems that the Geocoder library does not play well with the lib path for some reason. Although I have yet to investigate the specifics, removing or modifying this particular path resolved the issue for me.

If you happen to encounter a similar problem, chances are it could be related to your project's configuration settings.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In either _document.js or _app.js, make sure to include the <Head> component

When it comes to using <Head> from next/head should it be implemented within _app.js or should <Head> from next/document be utilized in _document.js instead? It can be confusing to determine the appropriate approach in this scenario. It's ...

Utilizing NextJs req.query parameter in Prisma for advanced query filtering

Currently, I am delving into the world of NextJS and experimenting with sending requests to an API while passing a parameter that is then used by Prisma to query the database. In order to achieve this, I've created a new file located at /api/posts/[s ...

PNG file is not displayed on React TSX page despite absence of any errors

I've implemented this initial design template from GitHub (https://github.com/vikpe/react-webpack-typescript-starter). Additionally, I'm utilizing react-bootstrap and have a container that includes a backgroundImage. const heroImage = require(&q ...

Utilizing Angular: Importing Scripts in index.html and Implementing Them in Components

Currently, I am attempting to integrate the Spotify SDK into an Angular application. While I have successfully imported the script from the CDN in index.html, I am encountering difficulties in utilizing it at the component level. It seems like there may be ...

Adding a static global constant in webpack dynamically

I'm facing a challenge with adding a global constant to my project using webpack.DefinePlugin. I've successfully added one in the module.exports, but I struggle to do this conditionally. When I declare and use '__VERSION__' in my module ...

Is there a way to retrieve the default ObjectId generated by MongoDB when using NextAuth?

Is it possible to access the MongoDB ObjectId of the user currently logged in when using Next Auth with services like Twitter? When signing in, Next Auth creates a new user, account, and session, but I'm unable to retrieve the _id value for my server ...

Troubleshooting problem in pre-rendering with Algolia connectSearchBox in NextJS React

I am currently developing a custom SearchBox component for Algolia instant search with react. Here is the code for my implementation: import { connectSearchBox } from "react-instantsearch-dom"; import InputText from "components/common/Input ...

Using Lodash to eliminate objects from a list

I have a specific model for my list, it looks like this: Animal Model id name age gender city Within the animals[] = []; array that I am working with, I need to remove the fields name, age, and gender while keeping id and city. How c ...

Guide to easily printing a page in Angular 4 using TypeScript

When using my web app, there are certain pages where I need to print only a specific component without including the sidebar. I have written the following TypeScript code to achieve this: print() { window.print(); } The relevant HTML code begins with: & ...

Saving in prettier differs from running it with npm

./file.ts (INCORRECT) import { jwtGroupClaimToRolesDomain, ScopeIds } from '@invison/shared'; import { Injectable, NestMiddleware } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { Response } fro ...

Compiling TypeScript: Using the `as` operator to convert and then destructure an array results in a compilation error, requiring an

I am currently utilizing TypeScript version 2.9.2. There is a static method in a third-party library called URI.js, which is declared as follows: joinPaths(...paths: (string | URI)[]): URI; I have a variable named urlPaths that is defined as urlPaths: s ...

Implement a system in Angular Service that automatically generates user IDs for an array of user inputs

How can I automatically increment the user ID for each new user input and use that value for deletion or updating of specific entries? If a user adds their details as shown in the output picture link below, I want to display the output in a similar format ...

Error encountered in Next.js: The function 'useLayoutEffect' was not successfully imported from 'react' (imported as 'React')

I've been in the process of migrating an application from CSR (using webpack only) to SSR, and I'm utilizing Next.js for this transition. Following the migration guide provided by Next.js for switching from vite (specifically focusing on parts r ...

When utilizing an object as state in React, the `setState` function will only set

Currently, I am working on developing a React form that utilizes a custom Input component which I have created multiple times. The objective is to gather all the names and values from the form in the parent component: {inputName: value, inputName2: valu ...

What exactly defines the nature of a type?

Have you ever wondered about the nature of a type? All this talk may seem perplexing, but sometimes code speaks louder than words. // Consider an interface interface A { } // Now, imagine a class that implements this interface. // There are several impl ...

Angular2 - Creating PDF documents from HTML content with jspdf

For my current project, I am in need of generating a PDF of the page that the user is currently viewing. To accomplish this task, I have decided to utilize jspdf. Since I have HTML content that needs to be converted into a PDF format, I will make use of th ...

Click on a link to open it in the current tab with customized headers

In my Angular project, I am attempting to open a link by clicking a button that redirects to the specified URL using the following code: window.open(MY_LINK, "_self"); However, in this scenario, I also need to include an access token in the header when t ...

Creating a TypeScript library with Webpack without bundling may seem like a daunting task, but

Currently, I am developing a React component package using Typescript and NPM. During my research, I discovered that generating individual .js and .d.ts files is more beneficial than bundling them into one bundle.js file. However, I am facing difficulty in ...

What is the best way to arrange a list in Angular2 using AngularFire2's FirebaseListObservable?

Looking to retrieve all users from a Firebase realtime database and organize them based on a score property. I managed to achieve this by using the variable users: FirebaseListObservable<any[]>; however, encountered the following errors: Type & ...

Combining all code in Electron using Typescript

I am currently working on developing a web application using Electron written in Typescript and I am facing some challenges during the building process. Specifically, I am unsure of how to properly combine the commands tsc (used to convert my .ts file to ...