gulp with nodemon, watch for file changes, “app crashed - waiting for file changes before starting”

橙三吉。 提交于 2019-12-11 10:47:04

问题


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.



来源:https://stackoverflow.com/questions/36972947/gulp-with-nodemon-watch-for-file-changes-app-crashed-waiting-for-file-chang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!