Listen EADDRINUSE: address already in use :::3000

Hi guys,

I’m stuck with this damn error, anyone solved this issue?,
I’m confidence that the code 100% is the same as Mosh taught.

Test Suites: 3 failed, 2 passed, 5 total
Tests: 3 failed, 25 passed, 28 total
Snapshots: 0 total
Time: 3.812 s
Ran all test suites.

Error number 1:
● /api/genres › GET / › should return all genres

listen EADDRINUSE: address already in use :::3000

  10 |
  11 | const port = process.env.PORT || 3000;
> 12 | const server = app.listen(port, () => winston.info(`Listening on port ${port}...`));
     |                    ^
  13 |
  14 | module.exports = server;

  at Function.listen (node_modules/express/lib/application.js:618:24)
  at Object.<anonymous> (index.js:12:20)
  at Object.<anonymous> (tests/integration/genres.test.js:9:33)

× should return 401 if client is not logged in (855 ms)

Error number 2:
● /api/returns › should return 401 if client is not logged in

listen EADDRINUSE: address already in use :::3000

  10 |
  11 | const port = process.env.PORT || 3000;
> 12 | const server = app.listen(port, () => winston.info(`Listening on port ${port}...`));
     |                    ^
  13 |
  14 | module.exports = server;

  at Function.listen (node_modules/express/lib/application.js:618:24)
  at Object.<anonymous> (index.js:12:20)
  at Object.<anonymous> (tests/integration/returns.test.js:12:18)

Error number 3:
● auth middleware › should return 401 if no token is provided

listen EADDRINUSE: address already in use :::3000

  10 |
  11 | const port = process.env.PORT || 3000;
> 12 | const server = app.listen(port, () => winston.info(`Listening on port ${port}...`));
     |                    ^
  13 |
  14 | module.exports = server;

  at Function.listen (node_modules/express/lib/application.js:618:24)
  at Object.<anonymous> (index.js:12:20)
  at Object.<anonymous> (tests/integration/auth.test.js:8:33)

Just info,

I already add “await” in the “server.close();” that Mosh taught and it didn’t works

Regret buying this shit outdated course
sorry bad words

I’m using windows, when starting the server correct me if i’m wrong.
I do this:
`1. set 1234=mySecureTest => (this is jwtPrivateKey for testing)
2. set NODE_ENV=test => (this is to set environtment for testing)

Am i right ?

Check if another instance of your server is already running on your machine. A port is an exclusive resource so only a single server can listen on it at any time.

This is why Mosh always makes a point of mentioning the exact package versions he’s installing. It’s also why I, as a long-term .NET guy, hate these more “linux” oriented technologies; they don’t seem to be able to go for longer than 5 minutes without breaking backward compatibility.

Jest seems to have had a change in the past few years and switched the default config from sequential composition to parallel. Why, on earth, they would do this for a technology frequently used for server integration testing is beyond me, but there it is. The fix is to simply add the runInBand option to the package.json jest command to ensure your tests are run sequentially:

"test": "jest --watchAll --verbose --runInBand"
6 Likes

@MarkF Thank you for the life saving tip !

1 Like

Thank you. Absolutely mind-boggling decision as you’ve stated. Saved a ton of time with this post.

Thank you MarkF, you helped me :+1:

Sounds like the --runInBand option loses you some performance (not relevant for tiny apps maybe, but I’m guessing in larger test suites it matters).

Seems like a good option is to alter your teardown to simple account for this by adding in ‘await’ even though I could swear the docs say ‘server.close()’ is a Callback and not a Promise or Async.

so …
afterEach( () => {
server.close();
});

becomes …
afterEach( async () => {
await server.close();
});

1 Like

these people just love bitching about colleges, courses etc why dont you learn all by yourself?