13

I have a very minimal gulpfile as follows, with a watch task registered:

var gulp = require("gulp");
var jshint = require("gulp-jshint");

gulp.task("lint", function() {
  gulp.src("app/assets/**/*.js")
    .pipe(jshint())
    .pipe(jshint.reporter("default"));
});

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

I cannot get the watch task to run continuously. As soon as I run gulp watch, it terminates immediately.

I've cleared my npm cache, reinstalled dependencies etc, but no dice.

$ gulp watch
[gulp] Using gulpfile gulpfile.js
[gulp] Starting 'watch'...
[gulp] Finished 'watch' after 23 ms
Kewin Dousse
  • 3,880
  • 2
  • 25
  • 46
amigolargo
  • 800
  • 2
  • 7
  • 24

2 Answers2

7

It's not exiting, per se, it's running the task synchronously.

You need to return the stream from the lint task, otherwise gulp doesn't know when that task has completed.

gulp.task("lint", function() {
  return gulp.src("./src/*.js")
  ^^^^^^
    .pipe(jshint())
    .pipe(jshint.reporter("default"));
});

Also, you might not want to use gulp.watch and a task for this sort of watch. It probably makes more sense to use the gulp-watch plugin so you can only process changed files, sort of like this:

var watch = require('gulp-watch');

gulp.task('watch', function() {
  watch({glob: "app/assets/**/*.js"})
    .pipe(jshint())
    .pipe(jshint.reporter("default"));
});

This task will not only lint when a file changes, but also any new files that are added will be linted as well.

OverZealous
  • 39,252
  • 15
  • 98
  • 100
  • This is working fine now, thanks! Confusion lay in 'starting watch', and 'finished watch' terminology. – amigolargo Mar 18 '14 at 10:25
  • For me `{glob: "app/assets/**/*.js"}` gives an error, just use `"app/assets/**/*.js"` instead. (NodeJS 6.3.1, Gulp 3.91, watch 4.3.9) – Ties Jul 31 '16 at 15:03
2

To add to OverZealous' answer which is correct.

gulp.watch now allows you to pass a string array as the callback so you can have two separate tasks. For example, hint:watch and 'hint'. You can then do something like the following.

gulp.task('hint', function(event){
    return gulp.src(sources.hint)
        .pipe(plumber())
        .pipe(hint())
        .pipe(jshint.reporter("default"));
})
gulp.task('hint:watch', function(event) {
   gulp.watch(sources.hint, ['hint']);
})

This is only an example though and ideally you'd define this to run on say a concatted dist file.

Jhey
  • 1,377
  • 7
  • 10
  • What is `sources.hint` in your example? An array of glob paths? Won't this lint _all_ files every time _one_ file changes? – oskarth Jan 28 '15 at 09:04
  • This would actually be out of date now too as `gulp-watch` wouldn't actually achieve the right result and would be easier to simply use gulps watch. The code above would indeed lint all files in the glob upon one file change. The best thing to do in this case is either concat your files and then you only need hint one. Or, you could have hint refer to a dist folder and use something like `gulp-change` to filter out the unchanged files. – Jhey Jan 29 '15 at 10:52