Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


server not stopping before restart when editing Gruntfile.js #22

pensierinmusica opened this Issue · 6 comments

3 participants



I have Grunt "watch" looking at some files and restarting Express if they get changed.

It all works seamlessly. For some weird reason though if the file edited is Gruntfile.js, grunt-express-server doesn't stop Express before relaunching when the express:dev task is triggered by grunt-watch and obviously it generates an EADDRINUSE error.

This is my grunt-watch configuration:

    watch: {
      options: {
        livereload: true
      stylus: {
        files: ['<%= %>/styles/sections/*.styl'],
        tasks: ['stylus'],
        options: {livereload: false}
      autoprefixer: {
        files: ['<%= %>/styles/*.css'],
        tasks: ['autoprefixer']
      express: {
        files: [
        tasks: ['express:dev'],
        options: {spawn: false} // Without this option specified express won't be reloaded

And this is my grunt-express-server configuration:

    express: {
      options: {
        port: process.env.PORT || 3000
      dev: {
        options: {
          script: 'app.js'
      prod: {
        options: {
          script: 'app.js',
          node_env: 'production'

The weird thing is that the problem ony happens with Gruntfile.js. If I modify, let's say, app.js or one of the controllers, the task correctly stops Express before reloading it.

Any thoughts?? Thanks!


I'm not really sure yet, but I have to ask, why restart the server if the Gruntfile changes?


I guess you're right... I could just live-reload the browser :)


I'm having this issue when I'm editing my package.json and wanting to reload the server to include new packages like middleware and the likes.


Wow, that's really odd! I was able to reproduce it, & trying to figure out why Gruntfile.js changes would cause this!


Apparently Gruntfile.js is auto-watched, which doesn't properly emit the proper signals to kill the server. As a result, the next iteration of watch seems to run the task in a new namespace, meaning the server never shuts down.

Trying an alternative now...


Aha! Stupid Grunt being clever. I'm having to introduce process._servers to keep the server in memory for stopping, since Grunt explicitly clears the require cache.

@ericclemmons ericclemmons closed this issue from a commit
@ericclemmons Store running servers on `process._servers[target]`.
Because modifications to Gruntfile.js automatically
clears `require.cache`, all server references were

Closes #22
@ericclemmons ericclemmons was assigned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.