64

I'm trying to use JSON Server in a React App. However, I keep getting the following error.

events.js:167
  throw er; // Unhandled 'error' event
  ^
Emitted 'error' event at:
at GetAddrInfoReqWrap.doListen [as callback] (net.js:1498:12)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:50:17)

I've upgraded npm and node to the latest versions. I've killed all processes with killall node and sudo killall -9 node

Any ideas what may be causing the issue?

frente_fin
  • 793
  • 2
  • 6
  • 9
  • Can you post the entire code ? – Dor Weid Jun 10 '18 at 10:23
  • Command and error listed below. Please let me know if anything else would be helpful to see. **Command:** json-server --watch db.json --port 3004 **Error:** `events.js:167 throw er; // Unhandled 'error' event ^ Error: getaddrinfo ENOTFOUND localhost at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:50:26) Emitted 'error' event at: at GetAddrInfoReqWrap.doListen [as callback] (net.js:1498:12) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:50:17)` – frente_fin Jun 10 '18 at 10:34
  • Ran into this and my issue was I was missing webpack-dev-server in package.json – bkaid Nov 11 '18 at 21:29
  • create-react-app my-app cd my-app npm install react-scripts@2.1.8 npm start – Thameem Jul 22 '19 at 07:31

25 Answers25

63

I had the same issue I ran killall node then npm start after that my local server at port 5000 worked again.

starball
  • 20,030
  • 7
  • 43
  • 238
Salma Elshahawy
  • 1,112
  • 2
  • 11
  • 21
49

Removing the node_modules directory and reinstalling again using npm install should solve the problem.

Alex Taylor
  • 8,343
  • 4
  • 25
  • 40
Mahdi Irani
  • 508
  • 3
  • 6
  • FWIW, I have the same problem and reinstallation did not help. A data point is that the problem does not manifest for me locally in MacOS, but happens when spinning up in a container in minikube. – user127 Jul 30 '18 at 17:24
  • 5
    Maybe removing the `package-lock.json` and `node_modules` and reinstalling packages will solve the problem – Mahdi Irani Aug 03 '18 at 11:11
  • 1
    deleting package-lock.json and node_modules solved the problem for me. – Dylan Oct 28 '18 at 01:17
  • 15
    this answer is applicable at least every 8 of 10 question about node – levye Apr 08 '19 at 22:30
  • I've had actually the same problem and `rm -rf node_modules` and `npm install` didn't fix it. I had to run `npm cache clear --force` as well. As it came up occasionally, I decided to write a [blog post](https://peterthaleikis.com/posts/how-to-fix-throw-er-unhandled-error-event) about it – spekulatius Jun 09 '19 at 04:24
  • for me just `npm i` worked – Zeeshan Ahmad Khalil Feb 07 '22 at 14:48
26

Try npm start as superuser: sudo npm start

This worked for me.

Peter Sobhi
  • 2,542
  • 2
  • 22
  • 28
9

For windows users, run command

Taskkill /IM node.exe /F

It works for me.

Reference:

https://stackoverflow.com/a/51408717/9033834

Karthikeyan Vellingiri
  • 1,270
  • 1
  • 17
  • 26
5

test another adb device ... real smartphone or android studie or genymotion npm start in your project open new tab and run react-native run-android and press ctrl + c in past terminal tab

i guess why this problem is we cant run http://localhost:8081/debugger-ui/

Mojtaba Darzi
  • 328
  • 5
  • 10
5

Had the same problem and none of the solutions above fixed it.

Server started on port 3000
node:events:368
      throw er; // Unhandled 'error' event
      ^

Error: getaddrinfo ENOTFOUND listening
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26)
Emitted 'error' event on Server instance at:
    at GetAddrInfoReqWrap.doListen [as callback] (node:net:1517:12)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:17) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'listening'
}

I did notice that node was trying to reach a host called 'listening'. So I added it to my hosts file and that "fixed" it.

127.0.0.1 listening
mkcons
  • 155
  • 1
  • 8
4

In my case, it worked after I followed the steps below:

  1. Delete node_modules folder.
  2. Run npm install.
  3. Run npm update.
  4. Restart the system. (may be this did the trick)
Prateek Kumar Dalbehera
  • 2,194
  • 3
  • 24
  • 30
3

I have the same issue. Basically, I am using node v10+ but my project was created in v8.11.4. All the modules were used according to the older version(v8), that's why I am facing the issue. So there are two solutions either update your module or downgrade the node.

I have solved by downgrading the node from v10 to v8. Because some of the modules are not possible to update due to project dependency.

Manish
  • 639
  • 6
  • 17
3

If you are running on Windows and have IIS running on your machine, ensure the IIS server is stopped if you are trying to run on port 8080. You can also change the port to not bring IIS into it.

npm start -- --port 8000
Vandelay Web
  • 137
  • 2
  • 9
3

restart the WebStorm was worked for me. instead of removed 'node_modules' folder and reinstall modules by npm install.

Levine Veblen
  • 121
  • 1
  • 5
2

I ran npm install and then npm update and it works again.

Eldlabs
  • 523
  • 7
  • 19
1

update severless with npm. This will resolve it.

Syed
  • 21
  • 1
1

Little late, but for any out there struggling, I came across this error by creating the React app without using sudo. Using sudo while running npx create-react-app app-name fixed the issue.

ChumiestBucket
  • 868
  • 4
  • 22
  • 51
1

Perhaps you started the echo server elsewhere with same port.

Ilya Degtyarenko
  • 1,359
  • 8
  • 16
1

I ran into this exact issue as well and found that it is only present with react-scripts@3.0.0, downgrading to react-scripts@2.1.8 resolved the issue without needing to mess with environment variables.

create-react-app my-app
cd my-app
npm install react-scripts@2.1.8
npm start
Pawan Verma
  • 1,152
  • 14
  • 22
1

Your site is running at http://localhost:8000 Try changing your port. Like 3000 or 8080.

app.listen(3000)

My 8080 post was taken, so I got this same error

1

Make sure you've closed the previous node connection before opening a new one on the same port.

Just try to close the earlier connection first, and then open the new one!

pie3636
  • 795
  • 17
  • 31
Akshay Chavan
  • 185
  • 1
  • 6
1

In my case the error was caused by a very large project with too many files, and then inotify cant check and needs to be raised the number of watches. This fix my error.

echo 1048576 > /proc/sys/fs/inotify/max_user_watches
Jorge Luis
  • 902
  • 12
  • 25
1

I was trying to install Vue CLI 4 (VueJS 3) app from the start. So, problem was in versions of node and npm. At current time the actual version of Node is 17. But for stable Vue CLI 4 needed Node >= 10.

I use nvm. So changin to downgraded Node worked for me:

nvm use 10.24.1
Galina
  • 369
  • 2
  • 6
1

Delete the node_modules folder and then run npm install. This solved the error for me.

Kaushal Banthia
  • 117
  • 1
  • 3
1

just kill the running server by tapping the delete button in visual studio and then run again the server.

npm start

Veerendra
  • 71
  • 1
  • 4
0

I have restored my hosts file to original state on Mac OS and it works for me. I edited this file yesterday and it made the problem. On this site you can copy the text of the original file https://pbxbook.com/other/mac-host.html

-1

Just run this command in your terminal

echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

Syed_Shahiq
  • 566
  • 6
  • 13
-1
sudo npm start

This should work for you

Shedrack
  • 656
  • 7
  • 22
-1

This worked for me

npm install react-scripts@2.1.8
npm start
Amr Tawfik
  • 79
  • 1
  • 5
  • This was already suggested in the Question comments as well as in another answer. I'd suggest to remove this Answer. – Boris May 31 '21 at 21:40