Gulp Watch only run once

This appears to be known issue I had the same problem and used the same as ddprrt. The difference was using directory glob (wildcard) as apposed to absolute path.

I changed this:

 gulp.task('watch', function() {
   gulp.watch('sass/shortcutcss/*.scss', ['sass'])
 });

to this:

 gulp.task('watch', function() {
   gulp.watch('sass/**/*.scss', ['sass'])
 });

For me it was adding a "return" to the task:

gulp.task('styles', function(){
 return gulp.src('css/styles.css')
    .pipe(autoprefixer())
    .pipe(gulp.dest('build'));
});

This problem made me crazy for a weekend. I tried all:

  • Add done()-Event
  • Rename/Touch/Clear target CSS
  • Be more specific with the filepaths

But the (reason for this problem and) the solution was so easy that I felt pathetic after that:

Just update your nodejs installation, mine was 0.12.x! No wonder that this doesn't worked.

After that the watch event works again. Sometimes it goes wrong again, too. But in this cases just save your file a second time and it get recognized. (I think foundation/gulp watch checks for changes to fast and while your file get replaced with the new uploaded one)


If you read the documentation closely, you see the following phrase:

You can pass plain callback, that will be called on every event or wrap it in gulp-batch to run it once

So, that's basically the deal with gulp-batch. To constantly watch it, just remove the batch call:

gulp.task('build', function (done) { 
    console.log('Working!'); 
    done();
});

gulp.task('watch', function () {
    watch('app/*.js', function () {
        gulp.start('build');
    });
});

(and add the 'done' callback to build to let Gulp know when you're finished).

Btw... I'm not sure, but I think gulp-watch is meant to not only watch files, but also directly returning a vinyl object. So actually using the built-in gulp.watch should have the same effect:

gulp.task('watch', function () {
    gulp.watch('app/**/*.js', ['build']);
});