Reputation: 175
I'm trying to automate a simple gulp task to run/debug node, watch for file changes, and restart node if any files change. Most popular recipes I've seen for this use gulp-nodemon
, but when a file change event occurs, (gulp-) nodemon
crashes:
[nodemon] app crashed - waiting for file changes before starting...
The crashing happens inconsistently, so sometimes I have to manually send a SIGINT
to stop the node process (which kind of defeats the purpose of nodemon).
I want to be able to run a gulp task that can watch files, run or debug node. How can this be accomplished without nodemon crashing?
Upvotes: 1
Views: 801
Reputation: 10109
It's not fancy, but the following should accomplish what you want.
'use strict'
const gulp = require('gulp');
const spawn = require('child_process').spawn;
gulp.task('debug', function() {
let child = spawn("node", ["debug", "./server.js"], { stdio: 'inherit' });
gulp.watch([__dirname + "/*.js", '!gulpfile.js'], function(event) {
console.log(`File %s was %s.`, event.path, event.type);
if (child) {
child.kill();
child = spawn("node", ["debug", "./server.js"], { stdio: 'inherit' });
}
});
});
This assumes you're watching for changes to any js
files in __dirname
except for your gulpfile.
Upvotes: 1