One of the most annoying thing when using the gulp.watch API is that it crashes
whenever errors happen and you will have to start it again manually. Gulp system
uses Nodejs Stream for automation, so you can use the events system of Nodejs
Stream to handle error.
For example, I have the following Gulp task
If one of the source .less file contains syntax error, gulp will crash while
watching these file. To handle it, simply add on('error') event after each
time you pipe the stream to a plugin.
Error Handling with gulp.watch and Browserify
Browserify API returns a stream that can be used with Gulp and we can also use
the on('error') event to handle error. However, browserify stream is a bit
different since you need to explicitly call the end() method of that stream,
otherwise, it cannot continue the watch process. The gulp task will look similar
to this
Utilities for Error Handling
You can use the two modules gulp-util and node-notifier for colorful
error message output in the console and send error notification if notification
system is available
And then in the on('error') event of the stream, pass the handler as the
callback
Note: on Mac, for the notification to work properly, you need to have an OSX
version that supports Notification center and
terminal-notifier installed.