George Mauer George Mauer - 5 months ago 43x
Node.js Question

Prevent errors from breaking / crashing gulp watch

I'm running gulp 3.6.2 and have the following task that was set up from a sample online

gulp.task('watch', ['default'], function () {[
], function (event) {
return gulp.src(event.path)

Any time there's an error in my CoffeeScript gulp watch stops - obviously not what I want.

As recommended elsewhere I tried this['./app/**/*.coffee'],['scripts']).on('error', swallowError);'./app/**/*.scss',['scss']).on('error', swallowError);
function swallowError (error) { error.end(); }

but it doesn't seem to work.

What am I doing wrong?

In response to @Aperçu's answer I modified my
method and tried the following instead:

gulp.task('scripts', function () {
.pipe(coffee({ bare: true }))
.on('error', swallowError);

Restarted, and then created a syntax error in my coffee file. Same issue:

[gulp] Finished 'scripts' after 306 μs

throw er; // Unhandled stream error in pipe.
Error: W:\bariokart\app\script\ error: unexpected *
at Stream.modifyFile (W:\bariokart\node_modules\gulp-coffee\index.js:37:33)
at (W:\bariokart\node_modules\gulp-coffee\node_modules\event-stream\node_modules\through\index.js:26:11)
at Stream.ondata (stream.js:51:26)
at Stream.EventEmitter.emit (events.js:95:17)
at queueData (W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\map-stream\index.js:43:21)
at next (W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\map-stream\index.js:71:7)
at W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\map-stream\index.js:85:7
at W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\lib\src\bufferFile.js:8:5
at fs.js:266:14
at W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\graceful-fs\graceful-fs.js:104:5
at Object.oncomplete (fs.js:107:15)


To me, your swallowError function should look like this :

function swallowError (error) {

  // If you want details of the error in the console


I think you have to bind this function on the error event of the task that was falling, not the watch task, because that's not where comes the problem, you should set this error callback on each task that may fail, like plugins that breaks when you have missed a ; or something else, to prevent watch task to stop.

Examples :

gulp.task('all', function () {
    .pipe(coffee({ bare: true }))
    .on('error', swallowError)

    .pipe(sass({ compass: true }))
    .on('error', swallowError)

Alternately, if you don't mind to include another module, you can use the log function of gulp-util to keep you from declare an extra function in your Gulpfile:

.on('error', gutil.log)

But I may recommend having a look at the awesome gulp-plumber plugin, which is used to remove the onerror handler of the error event, causing the break of the streams. It's very simple to use and it stops you from catch all the tasks that may fail.

  .pipe(coffee({ bare: true }))

More info about this on this article by the creator of the concerned plugin.