Search code examples
javascriptnode.jsherokudiscorddiscord.js

Discord bot not starting with heroku


So i have hosted my bot on heroku for some time now, and it worked just fine, but i recently moved, temporarly, to self hosting again cause i had to update to discord.js v13, but now i'm done with the changes, and i tried to re-host the bot on heroku, but it gives this error:

(node:4) UnhandledPromiseRejectionWarning: ReferenceError: AbortController is not defined

2021-08-08T20:30:17.770796+00:00 app[Worker.1]:     at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:172:15)

2021-08-08T20:30:17.770796+00:00 app[Worker.1]:     at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:176:19)

2021-08-08T20:30:17.770797+00:00 app[Worker.1]:     at RequestHandler.push (/app/node_modules/discord.js/src/rest/RequestHandler.js:50:25)

2021-08-08T20:30:17.770797+00:00 app[Worker.1]:     at processTicksAndRejections (internal/process/task_queues.js:95:5)

2021-08-08T20:30:17.770798+00:00 app[Worker.1]:     at async WebSocketManager.connect (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:128:9)

2021-08-08T20:30:17.770798+00:00 app[Worker.1]:     at async Client.login (/app/node_modules/discord.js/src/client/Client.js:245:7)

2021-08-08T20:30:17.770799+00:00 app[Worker.1]: (Use `node --trace-warnings ...` to show where the warning was created)

2021-08-08T20:30:17.770962+00:00 app[Worker.1]: (node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

2021-08-08T20:30:17.771045+00:00 app[Worker.1]: (node:4) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I don't understand what is happening, the only change that i think could cause this is the fact that i made an event handler and i now put events in their own files like commands, but i'm not sure if that has something to do with this error, i really hope you can help.


Solution

  • I recently came across this error too.

    Discord.js version 13 runs on node 16.6.1, however, the default Heroku node version is 14.x.

    That means we have to specify the version that we want Heroku to build our app on.

    In package.json add

    "engines": {
       "node": "16.x",
       "npm": "7.x"
    }
    

    Reference: https://devcenter.heroku.com/articles/nodejs-support#specifying-a-node-js-version

    This specifies to the buildpack that we need to use the latest version of node 16 and npm 7.

    However, after I added that, I came across a deployment error, and referred to this stack overflow question

    The answer there stated to add an empty ".npmignore" file in your root folder, and so I did that. However, after pushing everything to Github, I still received the same deployment error.

    It was saying that the file "node.exe" in a temp directory was missing. After some trial and error, I decided to commit and push the "node_modules/" directory to the Github (that took some time), because (sorry I didn't copy the build logs) the file "node.exe" resided in the "node_modules/" directory, and then re-deployed my application, and it worked!

    All in all it was kind of a roundhouse trip, because I don't know if the ".npmignore" file does anything (please comment if I'm wrong!).

    To recap what I did to fix my application:

    1. Add the engine specifiers to package.json
    2. Add .npmignore to the root directory (up to you if you want to add it)
    3. Commit and push the node modules folder to the Github page as well.
    4. Deploy through Github

    FYI: I don't know if this works on Heroku Git... tell me in the comments if it works.