Nnanyielugo
Nnanyielugo

Reputation: 403

Nodemon crashed when bound with gulp watch and restarted more than twice

I am trying to make my processes (webpack, nodemon-restart) work with a single gulp command. This works well enough. However, webpack builds only once if its task is tied to gulp's default task (together with nodemon), or embedded withing nodemon's gulp task.

Then I decided to tie both webpack build task and nodemon restart task to gulp's watch command and this works just the way I wanted, except that if you make changes and save them more than twice, the app nodemon crashed and prints this error in the console

"/home/nnanyielugo/Workspace/activity-calendar/node_modules/nodemon/lib/monitor/match.js:132
  var rules = monitor.sort(function (a, b) {
                     ^
    TypeError: Cannot read property 'sort' of undefined"

As a solution, i tried to tie the webpack build task to the nodemon restart using the .on() method, and instead got an infinite loop of restarting an rebuilding (nodemon restarts first, webpack builds, nodemon restarts again, webpack rebuilds, and on and on).

Does anyone have a solution please?`

Here is a sample of my code `

var gulp = require('gulp'),
    nodemon = require('gulp-nodemon'),
    webpack = require('webpack-stream');


gulp.task('default', ['watch']);

gulp.task('webpack', function() {
    return gulp.src('src/entry.js')
    .pipe(webpack(require('./webpack.config.js')))
    .pipe(gulp.dest('./public'));
});

gulp.task('nodemon', function () {
    return nodemon({
      script: 'app.js'
    , ext: 'js html'
    , env: { 'NODE_ENV': 'development' }
    })    
}) 

gulp.task('watch', function(){
    gulp.watch(['./api/**/*.js', './server/**/*.js', './*.js'],  ['webpack', 'nodemon']);
})`

Upvotes: 0

Views: 263

Answers (1)

David R
David R

Reputation: 15647

I guess, your nodemon and gulp's watch task collides with each other. Either you should get ride of using nodemon and to rely upon gulp to start your application.

Or else, you can get rid of your gulp's watch task and add the relevant script in your nodemon's restart method like this,

nodemon({
   // script goes here.
}).on('restart', your_reload_logic)

Hope this helps!

Upvotes: 0

Related Questions