Jon Fabian
Jon Fabian

Reputation: 286

Node.js fs.watchFile error EventEmitter memory leak detected

 (node) warning: possible EventEmitter memory leak detected. 11 listeners added.
 Use emitter.setMaxListeners() to increase limit.
 Trace:
     at StatWatcher.<anonymous> (events.js:139:15)
     at Object.watchFile (fs.js:762:8)
     at /home/gotimeco/gtnode2.js:79:11
     at Object.oncomplete (path.js:406:19)

I am using node.js v0.10.13. Any ideas ?

Upvotes: 1

Views: 1239

Answers (1)

Chandu
Chandu

Reputation: 4561

Please ensure fs.watchFile is not called multiple times on the same file.

Explaination: fs module contains a local object "statWatchers" {} to track files being watched.

So when fs.watchFile('fileX') is called a new statWatcher object is created if one does not exits and stored it in the "statWatchers" object against the file name. If there is already a statWatcher object associated with this file, it is returned.

statWatcher["fileX"] = new statWatcher();

And then calls addListener('change', listener) on the statWatcher object associated with this file.

Incase fs.watchFile is called for 11 times on "fileX", it will result in calling addListener on the statWatcher for event "change" 11 times.

EventEmitter throws an error if tried to add more than 11 listeners for the same event.

Hope the answer addressed your problem.

Upvotes: 3

Related Questions