question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"MaxListenersExceededWarning: Possible EventEmitter memory leak detected" when creating models with `autoCreate: true` before `mongoose.connect()`

See original GitHub issue

Do you want to request a feature or report a bug? Bug report

What is the current behavior? When creating more than 10 models MaxListenersExceededWarning appears

If the current behavior is a bug, please provide the steps to reproduce. Create more than 10 models like this:

mongoose.model<DocumentType, ModelType>(name, schema);

If you are using TypeScript, please include your tsconfig.json

{
  "compilerOptions": {
    "target": "es5"  ,
    "module": "commonjs"  ,
    "declaration": true  ,
    "declarationMap": true  ,
    "sourceMap": true  ,
    "outDir": "./dist"  ,
    "strict": true  ,
    "baseUrl": "./",
    "paths": {
      "@sm-errors": [
        "./common-library/errors"
      ],
      "@sm-microsoft-graph-api": [
        "./common-library/microsoft-graph-api"
      ],
      "@sm-shared-models": [
        "./common-library/shared-models"
      ],
      "@sm-shared-services": [
        "./common-library/shared-services"
      ],
      "@sm-lib": [
        "./common-library/lib"
      ]
    },
    "esModuleInterop": true  ,
    "skipLibCheck": true  ,
    "forceConsistentCasingInFileNames": true
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}

What is the expected behavior? Should create more models without warnings

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version.

mongoose: "^5.9.27",
Node 12
MongoDB 4.2.2

Currently I solved the issue by calling the following function each time before creating models:

mongoose.connection.setMaxListeners(MAX_LISTENERS_COUNT);

but I think there should be another solution for this.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
desercommented, Jan 14, 2021

It also happens when I use autoCreate: true on Schemas. Without that option it is ok

0reactions
IslandRhythmscommented, Mar 23, 2021

@vkarpov15 its very finicky but I can reproduce it but I’m not sure what triggers it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

App crash && Possible EventEmitter memory leak detected
I am trying to add a new feature to an existing (Node.js, express and MongoDB) project (Help pages). I began by adding two...
Read more >
[NODE-2123] MaxListenersExceededWarning caused by ...
I recently added `useUnifiedTopology: true` to my connection ... (node:23551) MaxListenersExceededWarning: Possible EventEmitter memory leak ...
Read more >
Mongoose v6.8.1: Connection
Returns a promise that resolves when this connection successfully connects to MongoDB, or rejects if this connection failed to connect.
Read more >
use emitter.setmaxlisteners() to increase limit - You.com
I'm getting the error warning: possible EventEmitter memory leak detected. 11 reconnect listeners added. Use emitter.setMaxListeners() to increase limit. app.
Read more >
Events | NestJS - A progressive Node.js framework
forRoot() call initializes the event emitter and registers any declarative event listeners that exist within your app. Registration occurs when the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found