gulp with nodemon, watch for file changes, “app cr

2019-07-29 21:19发布

问题:

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?

回答1:

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.



标签: node.js gulp