吞噬nodemon,观察文件更改,“应用程序崩溃 - 在启动之前等待文件更改”。 [英] gulp with nodemon, watch for file changes, "app crashed - waiting for file changes before starting"

查看:2055
本文介绍了吞噬nodemon,观察文件更改,“应用程序崩溃 - 在启动之前等待文件更改”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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...

崩溃发生得不一致,所以有时我必须手动发送一个 SIGINT 来停止节点进程(这种方式违背了nodemon的目的)。
我希望能够运行一个可以监视文件的gulp任务,运行或调试节点。如何在没有nodemon崩溃的情况下完成这项工作?

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?

推荐答案

这不是花哨的,但下面的内容应该可以完成你想要的功能。 >

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

假设您正在监视任何 js 文件位于 __ dirname 中,除了您的gulp文件。

This assumes you're watching for changes to any js files in __dirname except for your gulpfile.

这篇关于吞噬nodemon,观察文件更改,“应用程序崩溃 - 在启动之前等待文件更改”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆