# Listen EADDRINUSE: address already in use :::3000

**URL:** <https://forum.codewithmosh.com/t/listen-eaddrinuse-address-already-in-use-3000/7427>\
**Category:** Node\
**Created:** [September 20, 2021, 6:16am UTC](https://forum.codewithmosh.com/t/listen-eaddrinuse-address-already-in-use-3000/7427 "2021-09-20T06:16:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nushairfalahizz](https://avatars.discourse-cdn.com/v4/letter/n/ba9def/32.png) [@nushairfalahizz](https://forum.codewithmosh.com/u/nushairfalahizz)\
**Post date:** [September 20, 2021, 6:16am UTC](https://forum.codewithmosh.com/t/listen-eaddrinuse-address-already-in-use-3000/7427/1 "2021-09-20T06:16:16Z")

</div>

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)
```

---

<div class="post-metadata">

**Author:** ![nushairfalahizz](https://avatars.discourse-cdn.com/v4/letter/n/ba9def/32.png) [@nushairfalahizz](https://forum.codewithmosh.com/u/nushairfalahizz)\
**Post date:** [September 20, 2021, 6:18am UTC](https://forum.codewithmosh.com/t/listen-eaddrinuse-address-already-in-use-3000/7427/2 "2021-09-20T06:18:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nushairfalahizz](https://avatars.discourse-cdn.com/v4/letter/n/ba9def/32.png) [@nushairfalahizz](https://forum.codewithmosh.com/u/nushairfalahizz)\
**Post date:** [September 20, 2021, 6:22am UTC](https://forum.codewithmosh.com/t/listen-eaddrinuse-address-already-in-use-3000/7427/3 "2021-09-20T06:22:26Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [September 21, 2021, 6:23am UTC](https://forum.codewithmosh.com/t/listen-eaddrinuse-address-already-in-use-3000/7427/4 "2021-09-21T06:23:33Z")

</div>

> [@nushairfalahizz](#):
>
> `address already in use :::3000`

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.

---

<div class="post-metadata">

**Author:** ![MarkF](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@MarkF](https://forum.codewithmosh.com/u/MarkF)\
**Post date:** [September 22, 2021, 9:32am UTC](https://forum.codewithmosh.com/t/listen-eaddrinuse-address-already-in-use-3000/7427/5 "2021-09-22T09:32:55Z")

</div>

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:

```auto
"test": "jest --watchAll --verbose --runInBand"

```

---

<div class="post-metadata">

**Author:** ![dev-ous](https://avatars.discourse-cdn.com/v4/letter/d/4bbf92/32.png) [@dev-ous](https://forum.codewithmosh.com/u/dev-ous)\
**Post date:** [July 20, 2022, 10:30am UTC](https://forum.codewithmosh.com/t/listen-eaddrinuse-address-already-in-use-3000/7427/7 "2022-07-20T10:30:30Z")

</div>

@MarkF Thank you for the life saving tip !

---

<div class="post-metadata">

**Author:** ![mandatoryUserName](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@mandatoryUserName](https://forum.codewithmosh.com/u/mandatoryUserName)\
**Post date:** [September 5, 2022, 7:33pm UTC](https://forum.codewithmosh.com/t/listen-eaddrinuse-address-already-in-use-3000/7427/8 "2022-09-05T19:33:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Doniyor](https://avatars.discourse-cdn.com/v4/letter/d/35a633/32.png) [@Doniyor](https://forum.codewithmosh.com/u/Doniyor)\
**Post date:** [November 2, 2022, 6:58am UTC](https://forum.codewithmosh.com/t/listen-eaddrinuse-address-already-in-use-3000/7427/9 "2022-11-02T06:58:28Z")

</div>

Thank you MarkF, you helped me 👍

---

<div class="post-metadata">

**Author:** ![djb160](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/djb160/32/4881_2.png) [@djb160](https://forum.codewithmosh.com/u/djb160)\
**Post date:** [March 14, 2023, 5:04am UTC](https://forum.codewithmosh.com/t/listen-eaddrinuse-address-already-in-use-3000/7427/10 "2023-03-14T05:04:48Z")

</div>

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();  
});

---

<div class="post-metadata">

**Author:** ![karu](https://avatars.discourse-cdn.com/v4/letter/k/3da27b/32.png) [@karu](https://forum.codewithmosh.com/u/karu)\
**Post date:** [March 14, 2023, 1:58pm UTC](https://forum.codewithmosh.com/t/listen-eaddrinuse-address-already-in-use-3000/7427/11 "2023-03-14T13:58:14Z")

</div>

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