0

I read several StackOverflow posts on similar issues, but I still don't know what to do. I am using Node V16.9.1 and Discord.js V13 Here's my package.json:

{
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
  },
  "main": "index.js",
  "dependencies": {
    "@discordjs/builders": "^0.6.0",
    "@discordjs/rest": "^0.1.0-canary.0",
    "discord-api-types": "^0.23.1",
    "discord.js": "^13.1.0",
    "fs-extra": "^10.0.0"
  }
}

Here're the files in my project. And here's the Heroku log:

2022-01-10T19:57:45.000000+00:00 app[api]: Build started by user freddyzhang666@gmail.com
2022-01-10T19:58:02.000000+00:00 app[api]: Build succeeded
2022-01-10T19:58:02.088714+00:00 app[api]: Release v4 created by user freddyzhang666@gmail.com
2022-01-10T19:58:02.088714+00:00 app[api]: Deploy 0a32e378 by user freddyzhang666@gmail.com
2022-01-10T19:58:04.138779+00:00 heroku[worker.1]: State changed from crashed to starting
2022-01-10T19:58:06.544367+00:00 heroku[worker.1]: Starting process with command `node index.js`
2022-01-10T19:58:07.235596+00:00 heroku[worker.1]: State changed from starting to up
2022-01-10T19:58:07.912472+00:00 app[worker.1]: (node:4) UnhandledPromiseRejectionWarning: ReferenceError: AbortController is not defined
2022-01-10T19:58:07.912485+00:00 app[worker.1]: at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:172:15)
2022-01-10T19:58:07.912485+00:00 app[worker.1]: at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:176:19)
2022-01-10T19:58:07.912485+00:00 app[worker.1]: at RequestHandler.push (/app/node_modules/discord.js/src/rest/RequestHandler.js:50:25)
2022-01-10T19:58:07.912486+00:00 app[worker.1]: at async WebSocketManager.connect (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:128:9)
2022-01-10T19:58:07.912486+00:00 app[worker.1]: at async Client.login (/app/node_modules/discord.js/src/client/Client.js:245:7)
2022-01-10T19:58:07.912486+00:00 app[worker.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2022-01-10T19:58:07.912815+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)
2022-01-10T19:58:07.912854+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.
2022-01-10T19:58:07.915370+00:00 app[worker.1]: internal/modules/cjs/loader.js:905
2022-01-10T19:58:07.915371+00:00 app[worker.1]: throw err;
2022-01-10T19:58:07.915372+00:00 app[worker.1]: ^
2022-01-10T19:58:07.915372+00:00 app[worker.1]:
2022-01-10T19:58:07.915374+00:00 app[worker.1]: Error: Cannot find module '/app\anti_negativity.js'
2022-01-10T19:58:07.915375+00:00 app[worker.1]: Require stack:
2022-01-10T19:58:07.915375+00:00 app[worker.1]: - /app/index.js
2022-01-10T19:58:07.915375+00:00 app[worker.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
2022-01-10T19:58:07.915376+00:00 app[worker.1]: at Function.Module._load (internal/modules/cjs/loader.js:746:27)
2022-01-10T19:58:07.915376+00:00 app[worker.1]: at Module.require (internal/modules/cjs/loader.js:974:19)
2022-01-10T19:58:07.915376+00:00 app[worker.1]: at require (internal/modules/cjs/helpers.js:93:18)
2022-01-10T19:58:07.915377+00:00 app[worker.1]: at /app/index.js:13:23
2022-01-10T19:58:07.915377+00:00 app[worker.1]: at Array.forEach (<anonymous>)
2022-01-10T19:58:07.915377+00:00 app[worker.1]: at /app/index.js:11:11
2022-01-10T19:58:07.915378+00:00 app[worker.1]: at /app/node_modules/graceful-fs/graceful-fs.js:209:16
2022-01-10T19:58:07.915378+00:00 app[worker.1]: at FSReqCallback.oncomplete (fs.js:179:23) {
2022-01-10T19:58:07.915378+00:00 app[worker.1]: code: 'MODULE_NOT_FOUND',
2022-01-10T19:58:07.915378+00:00 app[worker.1]: requireStack: [ '/app/index.js' ]
2022-01-10T19:58:07.915379+00:00 app[worker.1]: }
2022-01-10T19:58:08.045930+00:00 heroku[worker.1]: Process exited with status 1
2022-01-10T19:58:08.308457+00:00 heroku[worker.1]: State changed from up to crashed
This is how I load a function from anti_negativity.js; this works perfectly fine on VS Code. The following is from index.js.

const fs = require("fs-extra");
const Discord = require('discord.js');
const client = new Client({ intents: [/* intents not shown */] });
client.commands = new Discord.Collection();

fs.readdir(__dirname, (_err, files) => {
    files.forEach(file => {
        if (!file.endsWith(".js")) return;
        let command = require(__dirname + "\\" + file);
        client.commands.set(command.name, command);
    });
});
// some code not shown
client.commands.get('anti_negativity').execute(oldMember, newMember, deletionLog.executor);
// some code not shown

client.login(/* token not shown */);
And the following is from anti_negativity.js.

// declarations not shown

module.exports = {
    name: 'anti_negativity',
    execute(oldMember, newMember, executor) {
        // code not shown
    }
}
Fred Zhang
  • 21
  • 3
  • How do you import `anti_negativity.js`? – Zsolt Meszaros Jan 10 '22 at 21:54
  • Hi @ZsoltMeszaros, I edited my post. Please let me know if you need more information! Thank you! :) – Fred Zhang Jan 10 '22 at 22:46
  • Apologies. I did not see this: https://stackoverflow.com/questions/68693319/why-am-i-getting-a-referenceerror-abortcontroller-is-not-defined-in-discord-js. I solved the problem by adding "engines": { "node": "16.9.1" } to my package.json – Fred Zhang Jan 11 '22 at 00:29
  • @FredZhang To use discord js v13 you need a node js version higher than 16, and this is not the default behavior of heroku when running an application. – Jorge Montejo Jan 11 '22 at 10:42
  • If you keep getting errors, you need to add `engines` in your `package.json` – 新Acesyyy Jan 15 '22 at 02:00

2 Answers2

0

I believe this is the same issue I faced when deploying my first bot to Heroku. If it is then adding

"engines": {
  "node": "17.x", // Or your desired version
  "npm": "8.x"    // Or your desired version
}

or simply

"engines": {
  "node": "17.x"  // Or your desired version once again
}

to your package.json file should solve the issue.

wolfy
  • 446
  • 4
  • 10
0

Hey there!

First of all, if you are using Typescript, whenever you are editing the bot use the command: tsc -w -p . (with the dot). This should be compiling the TypeScript files into JavaScript as you save changes to your files.

Now if you are using JavaScript (this will work with typescript too!) change your packages.json file to this below:

{
  "engines": {
    "node": "^17.4.0"
  },
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
  },
  "main": "index.js",
  "dependencies": {
    "@discordjs/builders": "^0.6.0",
    "@discordjs/rest": "^0.1.0-canary.0",
    "discord-api-types": "^0.23.1",
    "discord.js": "^13.1.0",
    "fs-extra": "^10.0.0"
  }
}

If this doesn't work, replicate your code files on https://codesandbox.io, obviously not putting in the bot token or anything like that and I'll see what I can do!

NanZee
  • 1