Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js fs.watchFile error EventEmitter memory leak detected

Node.js fs.watchFile error EventEmitter memory leak detected

Problem

 (node) warning: possible EventEmitter memory leak detected. 11 listeners added.
 Use emitter.setMaxListeners() to increase limit.
 Trace:
     at StatWatcher. (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 ?

Problem courtesy of: Jon Fabian

Solution

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.

Solution courtesy of: Chandu

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.js fs.watchFile error EventEmitter memory leak detected

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×