How to prevent grunt-nodemon from restarting all apps

150 views Asked by At

I'm running node on Windows 10. I have three node apps and I want to be able to start them all up with one handy grunt command. Furthermore, I want node to automatically restart if I modify any of the apps.

I'm using a combination of grunt-nodemon and grunt-concurrent for this. The node processes all start up fine.

The problem is that if I modify the code related to any of them they all restart, which takes a long time. How can I make it so that nodemon only restarts the app whose code I actually modified?

var loadGruntTasks = require('load-grunt-tasks')

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        concurrent: {
            runAll: {
                tasks: ['nodemon:app1', 'nodemon:app2', 'nodemon:app3'],
                options: {
                    logConcurrentOutput: true
                }
            }
        },

        nodemon: {
            app1: {
                script: './app1/app.js'
            },

            app2: {
                script: './app2/app.js'
            },

            app3: {
                script: './app3/app.js'
            }
        }
    })

    loadGruntTasks(grunt)
    grunt.registerTask('default', ['concurrent:runAll'])
}

Update

If I use grunt-watch instead of grunt-nodemon, only the app whose code I modified will restart. The problem is that grunt-watch only knows to run node app.js which gives an error because the app is already running. Is there a way to make grunt-watch kill the node process and restart it?

2

There are 2 answers

3
Patrick Motard On BEST ANSWER

I think the answer could be fairly simple. Nodemon has an ignore option. For each of your three applications nodemon grunt configurations you can configurate them to ignore the directories of the other applications. That way they only kick off their restart when their own files are changed and not those of other projects. Let me know how that goes. :) Specifics about setting up the ignore section of config can be found in both nodemons documentation and grunt-nodemons documentation.

0
d512 On

Patrick Motard's answer made me think about what directory nodemon was running in and how it was observing the files for changes. It appears that since I started grunt inside the parent directory of all the node apps that each nodemon process was looking for changes in all of those directories. So I set the working directory of the nodemon processes to the corresponding directory for each app using the options.cwd setting. That seemed to fix it. Here is the working solution:

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        concurrent: {
            runAll: {
                tasks: ['nodemon:app1', 'nodemon:app2', 'nodemon:app3'],
                options: {
                    logConcurrentOutput: true
                }
            }
        },

        nodemon: {
            app1: {
                script: 'app.js',
                options: {
                    cwd: './app1'
                }
            },

            app2: {
                script: 'app.js',
                options: {
                    cwd: './app2'
                }
            },

            app3: {
                script: 'app.js',
                options: {
                    cwd: './app3'
                }
            }
        }
    })

    loadGruntTasks(grunt)
    grunt.registerTask('default', ['concurrent:runAll'])
}