与nodemon一起吞咽,注意文件更改,"应用程序崩溃 - 在开始之前等待文件更改"

时间:2016-05-01 23:00:50

标签: node.js gulp

我尝试自动执行简单的gulp任务来运行/调试节点,监视文件更改,并在任何文件更改时重新启动节点。我使用过的最受欢迎的食谱gulp-nodemon,但是当发生文件更改事件时,(gulp-)nodemon崩溃了:

[nodemon] app crashed - waiting for file changes before starting...

崩溃发生的次数不一致,所以有时我必须手动发送SIGINT来停止节点进程(哪种方式违背了nodemon的目的)。 我希望能够运行可以监视文件,运行或调试节点的gulp任务。如果没有nodemon崩溃,怎么能实现呢?

1 个答案:

答案 0 :(得分:1)

这不是花哨,但以下应该可以达到你想要的效果。

  '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' });
        }
    });
});

这假设您正在关注js中除{gilpfile之外的任何__dirname文件的更改。