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

See original GitHub issue
(node:20128) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 field listeners added. Use emitter.setMaxListeners() to increase limit
(node:20128) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 file listeners added. Use emitter.setMaxListeners() to increase limit
(node:20128) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added. Use emitter.setMaxListeners() to increase limit
(node:20128) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 end listeners added. Use emitter.setMaxListeners() to increase limit

my code :

'use strict'

const fs = require('mz/fs')
const formidable = require('formidable')

module.exports = uploadFile

function uploadFile(opts) {
  opts = opts || {}

  const form = new formidable.IncomingForm(opts);
  form.uploadDir = opts.uploadDir ||  (os.tmpdir && os.tmpdir()) || os.tmpDir();
  fs.mkdir(form.uploadDir).then(function() {}).catch((err) => {});

  return async (ctx, next)=> {

    if(!ctx.req.get('Content-Type').includes('multipart/form-data')) return next();

    if (undefined !== ctx.req.body) return next()

    await form.parse(ctx.req.raw, function(err, fields, files) {
      if (err) throw err
      let filesObj = {};
      if(files){
          for(let f in files){
              let file = {};
              for(let k in files[f]){
                  if(['domain','size','path','name','type','hash','lastModifiedDate'].includes(k)){
                      file[k] = files[f][k];
                  }
              }
              filesObj[f] = file;
          }
      }
      ctx.req.body = fields
      ctx.req.files = filesObj
    })

    return next();
  }
}

Does anyone know why? and how to fix it ?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
GrosSacASaccommented, Feb 17, 2022

@cunha-ambisis you should create a new form on each request (inside app.use) See examples

0reactions
marianobenitezzcommented, Aug 22, 2022

Creating a new instance on each request worked for me. Thanks const formidable = new Formidable({ multiples: true, keepExtensions: true }) formidable.parse(req, (error, fields, files) => { console.log(fields) })

Before i defined the instance in the constructor of a class, and it throw that error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

possible EventEmitter memory leak detected - node.js
I'd like to point out here that that warning is there for a reason and there's a good chance the right fix is...
Read more >
Possible EventEmitter memory leak detected. · Issue #1295
Hi there, I think there is a regression in version 3.0. When I'm using Custom Agent (https) with keepAlive: true option and then...
Read more >
How to fix possible EventEmitter memory leak detected
The warning possible EventEmitter memory leak detected happens when you have more than 10 listeners (read EventEmitter) attached to an event ...
Read more >
Possible EventEmitter memory leak detected. 11 scanStop ...
The node red console indicates "MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 scanStop listeners added. Use emitter.
Read more >
Actions execution throws "MaxListenersExceededWarning"
(node:18) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [Socket]. Use emitter.
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