How to Fix "throw er; // Unhandled 'error' event"?
Engineer. Maker. Open Source Fan. Backpacker
by Peter Thaleikis
Some errors are easy and simple. Quick fixes you get resolved in minutes. Others aren't and you got to dig in for much longer than you would like to. This was one of these for me. I'm sharing my solution here to make it a bit easier for other developers.
This particular case problem arises while using my swiss armyknife build tool Laravel Mix on a project. When running npm run watch (actually yarn watch) I get this error message after the compile step is finish:
The line number depends on the exact libraries used in the project, but the events.js filename or throw er; // unhandled 'error' event are usually part of the error.
If supported, watch-poll often resolves the issue. But some libraries, like my starter for eleventy (this blog), aren't supporting poll and I needed to resort to a different approach. After spending some time researching and trying I've found a solution which works for now. I remove the node_modules-folder (rm -rf node_modules) and force-clear the NPM cache (npm cache clear --force) before reinstalling the node dependencies. Summarized in the following steps:
Please note: This removes also the lock-file. Thereby updating your project, make sure to test everything before you push the changes to production 🙏️
Update #1: Solution: Stop all other "watch"-processes
If you have a number of node processes running this might be part of your issue. I've noticed the problem coming up more often if I've got a number of watch processes (for other projects) running. After stopping these and clearing the files as described above the issues was resolved.
Update #2 (2020-03-04): Error: ENOSPC: System limit for number of file watchers reached, watch 'src/assets'
With a recent update of my dependencies, I've noticed a new, better understandable wording for the known error. This points you much faster in the right direction. Again, I've been able to resolve the issue using the approach mentioned above.
By chance I stumbled across a solution to increase the number of allowed file handles. This actually came with my recent trail of Microsoft Studio Code for Linux. It thrown a familiar sounding error:
Visual Studio Code is unable to watch for file changes in this large workspace (error ENOSPC)
Microsoft recommended the following to increase the number of allowed file handles. You can do by updating your /etc/sysctl.conf file. The detail steps are lined out here.
Update #4: Do you prefer a video?
More a video guy? Someone also made a video based on the content of article:
This might also come with a blocked port an app is try to listen to. In this case your error message might look like this:
node:events:505 throw er; // unhandled 'error' event ^ error: listen eaddrinuse: address already in use :::5000
The port at the end, here 5000, can vary.
If this looks like your problem, you might want to check what is listening on the port in question. Simply open the port with port, e.g. localhost:5000, in your browser. After turning of the application in question or changing your port this problem should be resolved.
By the way
I love the web and building stuff! The web still has lots amazing open-source libraries and actually innovative side-projects. Once in a while, I share these awesome web-findings via email. If this sounds like something you are into, subscribe below: