nodemon: pstree vuln / pstree upgrade / Error: listen EADDRINUSE: address already in use

  • nodemon -v: 1.18.6
  • node -v: 11.2.0
  • npm -v: 6.4.1
  • yarn -v: 1.12.3
  • OS / terminal: Ubuntu 18.10 cosmic (linux x64) / Bash
  • Command: nodemon -r ./lib app

I’m running into an EADDRINUSE error with nodemon and express since yesterday, which only happens when requiring a module (or file) using -r from the CLI.

Expected behaviour

It should restart the web server when the entry file is saved.

Actual behaviour

It hangs up with Error: listen EADDRINUSE: address already in use when the file is saved.

Steps to reproduce

  1. yarn init -y
  2. yarn add express and yarn add -D nodemon
  3. touch app.js and touch lib.js (see file contents below)
  4. dev script in package.json with nodemon -r ./lib app
  5. yarn dev
// app.js
require('express')().listen(3000, () => console.log('Works'))
// lib.js - empty
"scripts": {
  "dev": "nodemon -r ./lib app"
},
"dependencies": {
  "express": "^4.16.4"
},
"devDependencies": {
  "nodemon": "^1.18.6"
}

When I run yarn dev from the terminal, and then do Ctrl+S in app.js or lib.js in the editor, I get

yarn run v1.12.3
$ nodemon -r ./lib app
[nodemon] 1.18.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node -r ./lib app index.js`
Works
[nodemon] restarting due to changes...
[nodemon] starting `node -r ./lib app index.js`
events.js:167
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1294:14)
    at listenInCluster (net.js:1342:12)
    at Server.listen (net.js:1429:7)
    at Function.listen (/path/to/my/app/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/path/to/my/app/app.js:1:84)
    at Module._compile (internal/modules/cjs/loader.js:722:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
Emitted 'error' event at:
    at emitErrorNT (net.js:1321:8)
    at internalTickCallback (internal/process/next_tick.js:72:19)
    at process._tickCallback (internal/process/next_tick.js:47:5)
    at Function.Module.runMain (internal/modules/cjs/loader.js:778:11)
    at startup (internal/bootstrap/node.js:300:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:826:3)
[nodemon] app crashed - waiting for file changes before starting...

The app boots correctly, but as soon as any file changes, nodemon can’t restart it. In the meantime, the app still continues to run in the background. If I do Ctrl+C, it quits, but there’s no more process on port 3000, so killing it by port fuser -k 3000/tcp doesn’t do anything.

I found that

  • it doesn’t work with both yarn and npm, i.e. npm run dev produces the save error
  • it works when using require('./lib') in app.js instead of -r from the CLI
  • it works either way without express or any web server (because the port is not used)
  • same issue in Node 11.2.0, 11.1.0, and 10.13.0

I discovered this through esm when doing nodemon -r esm app, but later found out that it happens with any file. From that thread, the issue also seems to happen on MacOS as well. Tried rebooting, reinstalling Node, removing yarn.lock, then removing and re-installing node_modules, locking to older versions of nodemon, and express… I’m out of options here.

I scavenged many threads in this repo with this same error, but to no avail. I hope I’m posting in the right repo this time. Any help is appreciated, thanks!


P.S. Here’s the dump with nodemon -r ./lib app --dump just in case.

Click to expand
> app@1.0.0 dev /path/to/my/app
> nodemon -r ./lib app --dump

[nodemon] 1.18.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
--------------
node: v11.2.0
nodemon: 1.18.6
command: /home/alex/.nvm/versions/node/v11.2.0/bin/node /path/to/my/app/node_modules/.bin/nodemon -r ./lib app --dump
cwd: /path/to/my/app
OS: linux x64
--------------
{ run: false,
  system: { cwd: '/path/to/my/app' },
  required: false,
  dirs: [ '/path/to/my/app' ],
  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', './lib', 'app' ],
        scriptPosition: 3,
        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', './lib', 'app', 'index.js' ] },
    string: 'node -r ./lib app index.js' } }
--------------

About this issue

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

Commits related to this issue

Most upvoted comments

Same behavior with lts/boron and lts/carbon. Tried with nodemon 1.18.x & 1.17.x.

Also happens when nodemon --exec.

Debian 4.18.10-2 x86_64

Strange.

Edit: --signal SIGTERM or --delay 1500ms do not change behavior.

Edit2: I am now using a bootstrap.js instead of the -r flag yet I still get the above error when restarting (simple edit). There is a db conn and an http server in the app. It feels like the server holds onto the db conn and thus nodemon can’t terminate it.

The problem is in pstree.remy. It was recently published to 1.1.2, which broke nodemon for many people (#1463, #1464, #1466). nodemon depends on it via pstree.remy "^1.1.0". Note the ^ symbol; it tells npm to pull the latest 1.x.x, in this case, 1.1.2, and not 1.1.0.

If you go to yarn.lock, change from pstree.remy "^1.1.0" to pstree.remy "1.1.0" (no caret), remove pstree.remy@^1.1.0: block altogether, and re-run yarn, it installs the older working version, and reload will be back to normal. The props go to @jordie23 for finding this out.

@Himself65 you need to bump to nodemon@1.18.8

Thankfully I’ve landed on this issue and bumped to 1.18.7 - I was already on course to spending days on this!

(On Monday eve we’ve bumped to 1.18.6 but had to disable the tests in order to publish; last night we’ve bumped to 1.18.7 and reinstated the tests).

Have a look at the tests for the plugin and let me know if you think porting them here is viable.

Okay, I’m finally able to replicate. It needs nodemon to be spawned inside of an npm run command.