substack/watchify — pull request #32 by jballant (2014-03-25 17:47:57+00:00)
Title: Add option for watchers to allow you to share watchers between watchify instances

Thank you so much for this browserify plugin. In my project, we have an issue as the result of a large number of files sharing a large number of dependencies. These files are getting watched by each watchify instance. As a result of all these watchers being created a bunch of the same files, we are seeing this error everywhere:

```
(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
at StatWatcher.EventEmitter.addListener (events.js:160:15)
at Object.fs.watchFile (fs.js:1163:8)
at EventEmitter.FSWatcher._watch (/usr/local/lib/node_modules/modulename/node_modules/watchify/node_modules/chokidar/index.js:244:15)
at EventEmitter.FSWatcher._handleFile (/usr/local/lib/node_modules/modulename/node_modules/watchify/node_modules/chokidar/index.js:270:8)
at /usr/local/lib/node_modules/modulename/node_modules/watchify/node_modules/chokidar/index.js:356:15
at Object.oncomplete (fs.js:107:15)
```

This is because node maintains a private event emitter hash for all files the process is watching, so every shared dependency between watchify instances could be binding a listener to the same internal node watcher instance, despite being different chokidar watchers.

I think it makes sense to allow users to share watchers between watchify instances if they need to, if for no other reason than to preserve a bit of memory, but also to combat this error. Additionally, I think it would be a good idea to pair this change with the addition of a 'watch' event on the instance with the watcher and the dependency object so wrapper plugins can manage these watchers themselves, if desired.