Error: listen EADDRINUSE: address already in use :::3000 - Digitalocean

▼魔方 西西 提交于 2021-01-29 07:32:44

问题


I recently followed a video chat tutorial and wanted to host it on Digitalocean to test some webRTC things. I got it working and even got it hosted on Heroku. However, when I go to host it on an unused droplet (running off of the initial $100 free Digitalocean credit) and SSH into my droplet & run npm start (yes, although its not in the tutorials github code, npm start is in the package.json), I get an error.

USER***@nodejs-s-1vcpu-1gb-sfo2-01:~/DIRNAME$ sudo npm start

> livestream-server@0.0.1 start /home/USER***/DIRNAME
> node app.js

events.js:292
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1313:16)
    at listenInCluster (net.js:1361:12)
    at Server.listen (net.js:1447:7)
    at Object.<anonymous> (/home/USER***/ZMP_Solutions/app.js:17:8)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1340:8)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  code: 'EADDRINUSE',
  errno: 'EADDRINUSE',
  syscall: 'listen',
  address: '::',
  port: 3000
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! livestream-server@0.0.1 start: `node app.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the livestream-server@0.0.1 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

I already read read this related question and tried a lot of things, e.g. using sudo when running npm start, or setting the port to higher than 3000, but none of it helped. Is there a fix to this?

Thanks!


回答1:


Dont set port or higher. Because heroku or the digital ocean will dynamically assign port( whatever will be available)

const server = require('http').createServer();
const port = process.env.PORT || 3000;

server.listen(port, () => console.log(`Listening on ${port}`));



回答2:


This means you have a application already using port 3000. Maybe an instance of the same server.

You can close the other process as follows:

sudo netstat -lntp | grep 3000

to find the process which uses port 3000. Get the PID (Process ID) of that process. The PID is a number written like PID/ProcessName like 234/node in the output, where 234 is the PID. Use it in the following command:

sudo kill -9 PID

fill in the PID you found.

Now you can run your server normally and you won't have that error anymore.




回答3:


This issue actually had to do with the Firewall on Digitalocean. All I did was run sudo ufw disable, and it worked!!



来源:https://stackoverflow.com/questions/63681215/error-listen-eaddrinuse-address-already-in-use-3000-digitalocean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!