Nodemon: Nodemon restart on mac getting port in use

Created on 14 Dec 2018  Â·  16Comments  Â·  Source: remy/nodemon

  • nodemon -v: 1.18.7
  • node -v: v10.13.0
  • Operating system/terminal environment: Mac OS 10.13.x and VSC.
  • Using Docker? What image: no
  • Command you ran:
    nodemon -w server --exec \"babel-node server\"

Expected behaviour

When I am changing the file, it is expected to bring up the server again properly.
I am using visual studio code with prettier enabled on save(doubting it might be related to that though)

Actual behaviour

> nodemon -w server --exec "babel-node server"

[nodemon] 1.18.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /Users/krajiv/Maximus/maximus-fe-core/server/**/*
[nodemon] starting `babel-node server`
[Fri Dec 14 12:56:33 2018] com.ibm.diagnostics.healthcenter.loader INFO: Node Application Metrics 4.0.0.201812061506 (Agent Core 3.2.6)
[Fri Dec 14 12:56:34 2018] com.ibm.diagnostics.healthcenter.mqtt INFO: Connecting to broker localhost:1883
App started in port 5000

On making code change

App started in port 5000
[nodemon] restarting due to changes...
[nodemon] starting `babel-node server`
[Fri Dec 14 12:57:15 2018] com.ibm.diagnostics.healthcenter.loader INFO: Node Application Metrics 4.0.0.201812061506 (Agent Core 3.2.6)
[Fri Dec 14 12:57:15 2018] com.ibm.diagnostics.healthcenter.mqtt INFO: Connecting to broker localhost:1883
events.js:167
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::5000
    at Server.setupListenHandle [as _listen2] (net.js:1286:14)
    at listenInCluster (net.js:1334:12)
    at Server.listen (net.js:1421:7)
    at Function.listen (/Users/krajiv/Maximus/maximus-fe-core/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/Users/krajiv/Maximus/maximus-fe-core/server/index.js:38:7)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at loader (/Users/krajiv/Maximus/maximus-fe-core/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/krajiv/Maximus/maximus-fe-core/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at Object.<anonymous> (/Users/krajiv/Maximus/maximus-fe-core/node_modules/babel-cli/lib/_babel-node.js:154:22)
  …[[snip]]
[nodemon] app crashed - waiting for file changes before starting...

Steps to reproduce

Using nodemon with es6 server side(express code) that is converted using babel node and having prettier enabled on save on mac osx.

Dump output

node: v10.13.0
nodemon: 1.18.7
command: /usr/local/bin/node /Users/krajiv/Maximus/maximus-fe-core/node_modules/.bin/nodemon -w server --dump --exec babel-node server
cwd: /Users/krajiv/Maximus/maximus-fe-core
OS: darwin x64
--------------
{ run: false,
  system: { cwd: '/Users/krajiv/....' },
  required: false,
  dirs: [ '/Users/krajiv/...' ],
  timeout: 1000,
  options:
   { watch: [ 'server', re: /server/ ],
     dump: true,
     exec: 'babel-node server',
     ignore:
      [ '**/.git/**',
        '**/.nyc_output/**',
        '**/.sass-cache/**',
        '**/bower_components/**',
        '**/coverage/**',
        '**/node_modules/**',
        re: /.*.*\/\.git\/.*.*|.*.*\/\.nyc_output\/.*.*|.*.*\/\.sass\-cache\/.*.*|.*.*\/bower_components\/.*.*|.*.*\/coverage\/.*.*|.*.*\/node_modules\/.*.*/ ],
     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: null,
        exec: 'babel-node server',
        args: [],
        scriptPosition: null,
        nodeArgs: undefined,
        execArgs: [],
        ext: 'js,mjs,json',
        env: {} },
     monitor:
      [ '/Users/krajiv/......./**/*',
        '!**/.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: 'babel-node server', args: [] },
     string: 'babel-node server' } }

---

needs more info stale

Most helpful comment

The issues seems to occur because nodemon restarts the application before it was closed properly. I guess it sends the kill signal and instantly restarts the script. If your computer is fast enough, the process won't have the time to get killed, the new one will already have started, and crashed because of the port conflict.

Adding a small "delay": 500 in the configuration helps.

All 16 comments

Please upgrade to latest, this is fixed in that release.

nodemon -v
1.18.8

Still getting the same problem :(

(I've reopened the issue). Can you replicate this with a simpler pared down version of code? Something like:

require('http').createServer((req, res) => res.end('ok')).listen(8000);

If not, can you take your current code base and strip it down until you're able to replicate with as little code as possible so we have something to debug with?

Tried it with same code provided here

require('http')
  .createServer((req, res) => res.end('ok'))
  .listen(8000, () => {
    console.log('Starting app on 8000');
  });

Tried this from my code also

import express from 'express';
const app = express();
let port = 4000;
app.listen(port, () => {
  console.log(`App started in port ${port}`);
});

Still facing the same problem

[nodemon] restarting due to changes...
[nodemon] starting babel-node server
Starting app on 8000
[nodemon] restarting due to changes...
[nodemon] starting babel-node server
events.js:167
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::8000
    at Server.setupListenHandle [as _listen2] (net.js:1286:14)
    at listenInCluster (net.js:1334:12)
    at Server.listen (net.js:1421:7)
    at Object.<anonymous> (/Users/krajiv/Maximus/maximus-fe-core/server/index.js:3:4)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at loader (/Users/krajiv/Maximus/maximus-fe-core/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/krajiv/Maximus/maximus-fe-core/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
Emitted 'error' event at:
    at emitErrorNT (net.js:1313:8)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:11)
    at Object.<anonymous> (/Users/krajiv/Maximus/maximus-fe-core/node_modules/babel-cli/lib/_babel-node.js:154:22)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)`

I need a few things more to replicate (I still can't):

  • The contents of your package.json
  • The result of nodemon -v
  • From nodemon's installed location, the result of npm ls pstree.remy.

Here's the results I have:

{
  "name": "1491",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "babel-node server.js"
  },
  "license": "ISC",
  "dependencies": {
    "babel-cli": "^6.26.0"
  }
}

nodemon -v: 1.18.9

For pstree.remy version, you'll need to run it from inside the nodemon directory - if you've installed globally, you can try this command, but I can't guarantee it'll work on your system:

cd $(dirname $(which nodemon))/../lib/node_modules/nodemon
npm ls pstree.remy

This last command is important as it'll help determine how the process is leaving the server running.

Created new repo with basic files that itself it throwing me error, do note, I am using prettier with visual studio code.
https://github.com/rajivkr/nodemon-test

nodemon -v
1.18.9

I have installed nodemon as dev dependency in this project,

nodemon-test krajiv$ npm ls pstree.remy [email protected] /Users/krajiv/Node/nodemon-test └─┬ [email protected] └── [email protected]

For global location,
ATH032934:nodemon-test krajiv$ cd $(dirname $(which nodemon))/../lib/node_modules/nodemon ATH032934:nodemon krajiv$ npm ls pstree.remy [email protected] /usr/local/lib/node_modules/nodemon └── [email protected]

I also face the error. How can I help?

# $ /Users/mo/dev/there/node_modules/.bin/nodemon -v
1.18.8

Update: Removed and re-installed, looks like it's fixed with a few refreshes.
├─ [email protected]
└─ pstree.[email protected]

<3

@morajabi start by jumping to latest, then retest and then find the commonalities between your code and the sample repo and report your findings

@remy added new code changes in that sample repo, that is causing this to break, added appmetrics-prometheus for tracking my server details, when I comment this attach function, it is working fine. If this function is called, I am seeing this error. hope this can help in debugging,
https://github.com/CloudNativeJS/appmetrics-prometheus

@remy The issue is solved for me after

yarn remove nodemon

and then:

yarn add nodemon

Didn't change any code.

I encountered the same problem with lastest nodemon too.
I solved the this problem after uninstall and install nodemon again
thanks @morajabi 's solution

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

Changed code logic to include prometheus only when app is in prod mode. Not facing this issue after that. Thanks @remy

The issues seems to occur because nodemon restarts the application before it was closed properly. I guess it sends the kill signal and instantly restarts the script. If your computer is fast enough, the process won't have the time to get killed, the new one will already have started, and crashed because of the port conflict.

Adding a small "delay": 500 in the configuration helps.

@OzoneGrif I agree with your assumption, I just came across this issue and your suggestion solved it for me.

I am kinda CMD+Sbig fan, so I press it twice or three times (thanks for sticky mac keyboard it forced me to have this bad habit), when I do so, I run into this issue. having the delay solved it.

Thanks.

The issues seems to occur because nodemon restarts the application before it was closed properly. I guess it sends the kill signal and instantly restarts the script. If your computer is fast enough, the process won't have the time to get killed, the new one will already have started, and crashed because of the port conflict.

Adding a small "delay": 500 in the configuration helps.

In my case only adding 3 second delay fixed the issue. Thanks anyway.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jagged3dge picture jagged3dge  Â·  4Comments

Bastorx picture Bastorx  Â·  5Comments

hkeio picture hkeio  Â·  3Comments

olalonde picture olalonde  Â·  3Comments

dimsmol picture dimsmol  Â·  4Comments