1

I'm trying to host a Discord bot for the first time through Heroku (also my first time using the site). It was running perfectly locally, but I started receiving errors in regards to the $PORT once I went online. When I looked that up, I saw I needed to listen to the server in order for the port to stick, but every attempt I've done results in the EADDRINUSE error. I've looked over and tried many examples through these previous answers here and here, deleted modules and reinstalled them countless times, ended node.exe processes before booting up, but I keep running into the error. I must be missing something but I can't figure it out and I feel like I'm just running in circles. Any help would be appreciated.

The error report:

2020-07-28T21:24:01.605923+00:00 heroku[web.1]: Starting process with command `npm start`
2020-07-28T21:24:03.399990+00:00 app[web.1]: 
2020-07-28T21:24:03.400006+00:00 app[web.1]: > bot@1.0.0 start /app
2020-07-28T21:24:03.400006+00:00 app[web.1]: > node bot.js
2020-07-28T21:24:03.400006+00:00 app[web.1]: 
2020-07-28T21:24:04.323323+00:00 app[web.1]: Ready!
2020-07-28T21:24:06.981374+00:00 app[web.1]: Listening on 50667
2020-07-28T21:24:07.141720+00:00 app[web.1]: events.js:292
2020-07-28T21:24:07.141721+00:00 app[web.1]:       throw er; // Unhandled 'error' event
2020-07-28T21:24:07.141722+00:00 app[web.1]:       ^
2020-07-28T21:24:07.141722+00:00 app[web.1]: 
2020-07-28T21:24:07.141724+00:00 app[web.1]: Error: listen EADDRINUSE: address already in use :::50667
2020-07-28T21:24:07.141725+00:00 app[web.1]:     at Server.setupListenHandle [as _listen2] (net.js:1313:16)
2020-07-28T21:24:07.141725+00:00 app[web.1]:     at listenInCluster (net.js:1361:12)
2020-07-28T21:24:07.141726+00:00 app[web.1]:     at Server.listen (net.js:1447:7)
2020-07-28T21:24:07.141727+00:00 app[web.1]:     at Function.listen (/app/node_modules/express/lib/application.js:618:24)
2020-07-28T21:24:07.141727+00:00 app[web.1]:     at Client.<anonymous> (/app/bot.js:21075:4)
2020-07-28T21:24:07.141727+00:00 app[web.1]:     at Client.emit (events.js:315:20)
2020-07-28T21:24:07.141743+00:00 app[web.1]:     at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
2020-07-28T21:24:07.141745+00:00 app[web.1]:     at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
2020-07-28T21:24:07.141752+00:00 app[web.1]:     at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
2020-07-28T21:24:07.141752+00:00 app[web.1]:     at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:436:22)
2020-07-28T21:24:07.141753+00:00 app[web.1]: Emitted 'error' event on Server instance at:
2020-07-28T21:24:07.141753+00:00 app[web.1]:     at emitErrorNT (net.js:1340:8)
2020-07-28T21:24:07.141753+00:00 app[web.1]:     at processTicksAndRejections (internal/process/task_queues.js:84:21) {
2020-07-28T21:24:07.141754+00:00 app[web.1]:   code: 'EADDRINUSE',
2020-07-28T21:24:07.141754+00:00 app[web.1]:   errno: 'EADDRINUSE',
2020-07-28T21:24:07.141755+00:00 app[web.1]:   syscall: 'listen',
2020-07-28T21:24:07.141755+00:00 app[web.1]:   address: '::',
2020-07-28T21:24:07.141756+00:00 app[web.1]:   port: 50667
2020-07-28T21:24:07.141756+00:00 app[web.1]: }
2020-07-28T21:24:07.154255+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-07-28T21:24:07.154464+00:00 app[web.1]: npm ERR! errno 1
2020-07-28T21:24:07.157313+00:00 app[web.1]: npm ERR! bot@1.0.0 start: `node bot.js`
2020-07-28T21:24:07.157445+00:00 app[web.1]: npm ERR! Exit status 1
2020-07-28T21:24:07.157646+00:00 app[web.1]: npm ERR! 
2020-07-28T21:24:07.157759+00:00 app[web.1]: npm ERR! Failed at the bot@1.0.0 start script.
2020-07-28T21:24:07.157832+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-07-28T21:24:07.169750+00:00 app[web.1]: 
2020-07-28T21:24:07.169882+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-07-28T21:24:07.169957+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-07-28T21_24_07_159Z-debug.log
2020-07-28T21:24:07.254000+00:00 heroku[web.1]: State changed from starting to crashed
2020-07-28T21:24:07.225012+00:00 heroku[web.1]: Process exited with status 1

package.json

{
  "name": "bot",
  "version": "1.0.0",
  "lockfileVersion": 1,
  "requires": true,
  "dependencies": {
    "discord.js": "^12.2.0",
    "exit-hook": "^2.2.0",
    "express": "^4.17.1",
    "http": "0.0.1-security",
    "webpack-dev-server": "^3.11.0"
  },
  "scripts": {
    "start": "node bot.js",
    "stop-win": "Taskkill /IM node.exe /F"
  },
  "main": "bot.js",
  "devDependencies": {},
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": ""
}

Current code (though I have tried many different ones as well.)

const express = require('express');
const path = require('path');
const PORT = process.env.PORT || 5000;

express()
  .use(express.static(path.join(__dirname, 'public')))
  .set('views', path.join(__dirname, 'views'))
  .set('view engine', 'ejs')
  .get('/', (req, res) => res.render('pages/index'))
  .listen(PORT, () => console.log(`Listening on ${ PORT }`));


},);

I understand that I should probably run the bot as a Worker instead of Web through Heroku, but the option is not available to me for some reason and my Procfile was doing nothing. I know there must be something I'm overlooking. When I run the stop-win script, it kills two node.exe processes even when I try running the code only once. I've been looking at this for too long (days!) and keep going in circles, I need some fresh eyes and ideas. Please help!

EDIT: Thanks Tin Nguyen for pointing me in the right direction for the Procfile! I was able to use this thread to make an echo, and thus a functioning Procfile making it a worker. This let me fix all of the mess. Thank you very much!

  • You are listening on the `$PORT` multiple times. You are developing a discord bot which does not require a website (except you have written html, js and css code for your discord bot...). You should use a worker dyno instead of a web dyno. If your `Procfile` was not working than your `Procfile` was faulty. Make sure your `Procfile` has no file ending `Procfile.txt` is not valid. – Tin Nguyen Jul 29 '20 at 06:47

1 Answers1

0

I think the error is comming from the port! look at the errors,

Ready!
Listening on 50667
events.js:292
throw er; // Unhandled 'error' event
Error: listen EADDRINUSE: address already in use :::50667

it says "address already in use", try to change it and let me noticied if it works!

Florian Lepage
  • 134
  • 1
  • 1
  • 8