Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gulp reload on gulpfile.js change
// Gulp restart when gulpfile is changed
var spawn = require('child_process').spawn;
gulp.task('gulp-autoreload', function() {
// Store current process if any
var p;
gulp.watch('gulpfile.js', spawnChildren);
// Comment the line below if you start your server by yourslef anywhere else
spawnChildren();
function spawnChildren(e) {
if(p) {
p.kill();
}
p = spawn('gulp', ['build'], {stdio: 'inherit'});
}
});
gulp.task('build', function() {
// Your stuff here with build
// Moreover, it's a good idea to have livereload if necessary
})
@yoannmoinet

This comment has been minimized.

Copy link

@yoannmoinet yoannmoinet commented Oct 22, 2015

Very useful, thanks ! 👍

@marcolino

This comment has been minimized.

Copy link

@marcolino marcolino commented Oct 30, 2015

When / where is task gulp-autoreload started? I see no dependency on it... Just adding this to my gulpfile.js does not runs gulp-autoreload on gulpfile change, and gulpfile is not reloaded... :-(

@marcolino

This comment has been minimized.

Copy link

@marcolino marcolino commented Oct 30, 2015

Adding

gulp.watch('gulpfile.js', [ 'gulp-autoreload' ]);

Does the trick, sorry... :-)

One minor issue: I get a livereload error (EADDRINUSE) since I'd need to stop it before restarting gulp...
Could you add a livereload stop to your code?

@checktravis

This comment has been minimized.

Copy link

@checktravis checktravis commented Dec 20, 2015

this was very helpful. i did get stuck on an error. turns out on windows, you have to use gulp.cmd not gulp in the spawn function call. also, when i added the gulpfile.js watch task to my existing task with my other watches, it caused a loop. i had to put this watch task by itself and that seemed to do the trick.

@tilap

This comment has been minimized.

Copy link
Owner Author

@tilap tilap commented Dec 29, 2015

@marcolino: sorry didn't put the watch task coz it depends on the way you work with gulp. However, you don't have to create one more watcher task as it is already in the gulp-autoreload task. Just add it as a standard gulp task dependancy.

Here a usecase example - old code but still good for that example: https://github.com/tilap/frast/blob/master/gulpfile.js#L399

@checktravis: yes in a separated task is better. Look at the example too it may help you.

@evilgeniuscreative

This comment has been minimized.

Copy link

@evilgeniuscreative evilgeniuscreative commented May 12, 2016

I don't know nodejs (yet) but am working with Gulp.

I can't get this to work; I keep getting the following. Thoughts?

events.js:141
throw er; // Unhandled 'error' event
^

Error: spawn gulp ENOENT
at exports._errnoException (util.js:855:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:178:32)
at onErrorNT (internal/child_process.js:344:16)
at nextTickCallbackWith2Args (node.js:455:9)
at process._tickCallback (node.js:369:17)

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented May 13, 2016

@evilgeniuscreative - and all fellow window users.

Please note that this line
-> p = spawn('gulp', ['build'], {stdio: 'inherit'});

Will not work on Windows 10 with node version 5.7.0 - gulp 4 alpha.
due to the following issue

I had to use the following to make it work.
-> p = spawn('gulp.cmd', ['build'], {stdio: 'inherit'});

Hope this helps.

@Sphinxxxx

This comment has been minimized.

Copy link

@Sphinxxxx Sphinxxxx commented Mar 24, 2018

New behavior in gulp v4:

The watcher function (spawnChildren()) is passed a callback argument which must be called after spawnChildren() is done, or else the gulp-autoreload task never goes back to watching for further changes (i.e. the reload only works once).

function spawnChildren(callback) {
    if(p) { p.kill(); }
    p = spawn('gulp', ['build'], { stdio: 'inherit' });

    if(callback) { callback(); }
}

https://github.com/gulpjs/gulp/blob/v4.0.0/docs/API.md#fn-1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment