nodemon: Nodemon does not wait for process to exit before starting a new one

System information:

$ nodemon -v
1.18.9
$ node -v
v11.6.0
$ uname -a
Linux formative 4.13.0-43-generic #48~16.04.1-Ubuntu SMP Thu May 17 12:56:46 UTC 2018 x86_64 
x86_64 x86_64 GNU/Linux

Expected behaviour

When I change a file, nodemon signals the process but does not start a new process unless the existing process exits.

Actual behaviour

nodemon starts a new process while the old process continues to run. If the old process was listening on a port, the new process might get an “address in use” error.

Steps to reproduce

Check out this git repo and follow the instructions:

https://github.com/dobesv/nodemon-repro

Alternatively, just run any process that uses the spawn method of launching (or maybe pass --spawn ?) and does not exit when it receives SIGUSR2.

Dump

$ nodemon -r ./register index.js --dump
[nodemon] 1.18.9
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
--------------
node: v11.6.0
nodemon: 1.18.9
command: /home/ubuntu/.nvm/versions/node/v11.6.0/bin/node /home/ubuntu/nodemon-repro/node_modules/.bin/nodemon -r ./register index.js --dump
cwd: /home/ubuntu/nodemon-repro
OS: linux x64
--------------
{ run: false,
  system: { cwd: '/home/ubuntu/nodemon-repro' },
  required: false,
  dirs: [ '/home/ubuntu/nodemon-repro' ],
  timeout: 1000,
  options:
   { dump: true,
     ignore:
      [ '**/.git/**',
        '**/.nyc_output/**',
        '**/.sass-cache/**',
        '**/bower_components/**',
        '**/coverage/**',
        '**/node_modules/**',
        re: /.*.*\/\.git\/.*.*|.*.*\/\.nyc_output\/.*.*|.*.*\/\.sass\-cache\/.*.*|.*.*\/bower_components\/.*.*|.*.*\/coverage\/.*.*|.*.*\/node_modules\/.*.*/ ],
     watch: [ '*.*', re: /.*\..*/ ],
     ignoreRoot:
      [ '**/.git/**',
        '**/.nyc_output/**',
        '**/.sass-cache/**',
        '**/bower_components/**',
        '**/coverage/**',
        '**/node_modules/**' ],
     restartable: 'rs',
     colours: true,
     execMap: { py: 'python', rb: 'ruby' },
     stdin: true,
     runOnChangeOnly: false,
     verbose: false,
     signal: 'SIGUSR2',
     stdout: true,
     watchOptions: {},
     execOptions:
      { script: 'index.js',
        exec: 'node',
        args: [ '-r', './register' ],
        scriptPosition: 2,
        nodeArgs: undefined,
        execArgs: [],
        ext: 'js,mjs,json',
        env: {} },
     monitor:
      [ '*.*',
        '!**/.git/**',
        '!**/.nyc_output/**',
        '!**/.sass-cache/**',
        '!**/bower_components/**',
        '!**/coverage/**',
        '!**/node_modules/**' ] },
  load: [Function],
  reset: [Function: reset],
  lastStarted: 0,
  loaded: [],
  watchInterval: null,
  signal: 'SIGUSR2',
  command:
   { raw:
      { executable: 'node', args: [ '-r', './register', 'index.js' ] },
     string: 'node -r ./register index.js' } }
--------------
Done in 0.23s.

Related issues

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 15
  • Comments: 28 (6 by maintainers)

Most upvoted comments

This is still an issue

This is an issue for me too on Mac OSX

Found a solution for this. Create a nodemon.json file in you’re project root directory. add the following to it: { “events”:{ “restart”:"fuser -k [port number] /tcp " } } This will ensure that everytime nodemon is restarted , it kills the process running on it’s assigned port and then restarts you’re server. Alternatively you can install npm package kill-port to do the same thing as fuser

I actually think, that this is a duplicate of #1476 and should also be fixed by the PR #1579 . Can someone confirm this?

PR live in nodemon@2.0.0

@AndrewOdiit this is a nice solution, I’m going to add this to the faq too 👍

To workaround this issue you have to avoid using the spawn launch method. What I found solved the issue for my particular case is to use NODE_OPTIONS to pass options to node, instead of passing them to nodemon as command line argument. For example NODE_OPTIONS="-r require.js --max_old_space_size=1000" nodemon script.js. This way it only creates one process with no intermediate sh process, so it correctly waits for the script to exit before restarting.

Rolling back to nodemon@1.18.7 fixed the issue for me. I remember this issue was happening before, was resolved, and appears to have reoccurred in the latest version.