Skip to content
Advertisement

ReferenceError: require is not defined MongoDB

I’m trying to connect to my MongoDB database and I’m getting this error

JavaScript
JavaScript

Advertisement

Answer

You are attempting to use require() inside an ESM module (you can see the Object.loadESM in the call stack of the error) which tells us it’s an ESM module. You can’t use require() in that type of module. Instead, you must use import.

So, you probably want:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement