Discord bot UnhandledPromiseRejectionWarning in production ( HEROKU) [duplicate] - node.js

This question already has answers here:
Why am I getting a ReferenceError: AbortController is not defined in Discord.js v13?
(3 answers)
Closed 1 year ago.
I deployed my discord bot to Heroku but It is giving an error of unhandled promise rejection
Here are the logs
2021-09-10T20:50:26.000000+00:00 app[api]: Build started by user codertanishq#gmail.com
2021-09-10T20:50:45.558185+00:00 app[api]: Deploy c608c194 by user codertanishq#gmail.com
2021-09-10T20:50:45.558185+00:00 app[api]: Release v11 created by user codertanishq#gmail.com
2021-09-10T20:50:46.000000+00:00 app[api]: Build succeeded
2021-09-10T20:50:46.978335+00:00 heroku[web.1]: State changed from crashed to starting
2021-09-10T20:50:48.877754+00:00 heroku[web.1]: Starting process with command `npm start`
2021-09-10T20:50:51.529490+00:00 app[web.1]:
2021-09-10T20:50:51.529505+00:00 app[web.1]: > start#1.0.0 start /app
2021-09-10T20:50:51.529506+00:00 app[web.1]: > node ./src/bot.js
2021-09-10T20:50:51.529506+00:00 app[web.1]:
2021-09-10T20:50:51.762757+00:00 app[web.1]: (node:22) UnhandledPromiseRejectionWarning: ReferenceError: AbortController is not defined
2021-09-10T20:50:51.762758+00:00 app[web.1]: at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:172:15)
2021-09-10T20:50:51.762758+00:00 app[web.1]: at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:176:19)
2021-09-10T20:50:51.762758+00:00 app[web.1]: at RequestHandler.push (/app/node_modules/discord.js/src/rest/RequestHandler.js:50:25)
2021-09-10T20:50:51.762759+00:00 app[web.1]: at async WebSocketManager.connect (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:128:9)
2021-09-10T20:50:51.762759+00:00 app[web.1]: at async Client.login (/app/node_modules/discord.js/src/client/Client.js:245:7)
2021-09-10T20:50:51.762759+00:00 app[web.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2021-09-10T20:50:51.763064+00:00 app[web.1]: (node:22) 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-09-10T20:50:51.763091+00:00 app[web.1]: (node:22) [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.
2021-09-10T20:50:51.847328+00:00 heroku[web.1]: Process exited with status 0
2021-09-10T20:50:51.902780+00:00 heroku[web.1]: State changed from starting to crashed

You would have to upgrade your node version. You may add the following to your package.json:
"engines": {
"node": "16.9.1",
}
And redeploy your project and it would work

Discord.js v13 needs AbortController to function, but it was only introduced in node versions 16 and above. You have two options to fix this error:
Upgrade your node version on heroku to 16 or above
Use a polyfill for your AbortController , AbortController-polyfill is a package to do that, you may utilise it like so:
const { AbortController } = require('abortcontroller-polyfill/dist/cjs-ponyfill');
global.AbortController = AbortController;

Related

Can I ignore unhandledRejection warning of node JS while deployment with CI Jenkins?

I am getting below warning message while building my react js project using CI
npm build fails with below message:
npm run build command contains --trace-warnings. But it has not trace exact location of warning.
(node:179) UnhandledPromiseRejectionWarning: Failed to compile server
at emitUnhandledRejectionWarning (internal/process/promises.js:170:15)
at processPromiseRejections (internal/process/promises.js:247:11)
at processTicksAndRejections (internal/process/task_queues.js:96:32) (node:179)
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: 1) (node:179) [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.
at emitDeprecationWarning (internal/process/promises.js:180:11)
at processPromiseRejections (internal/process/promises.js:249:13)
at processTicksAndRejections (internal/process/task_queues.js:96:32)
Is there a way I can ignore the warning during this process as I cannot find out the cause of this warning and not able to fix this.
Node version 10.16.3
npm version. 6.9.0
Using Jenkins for CI.

How to see stacktrace / cause of an error in Jest?

I'm trying to debug a nodeJS app.
I have some code which causes an error, a variable is undefined.
When I run the code normally, the error is very clear and easy to find:
without jest:
➜ server git:(dc/build) ✗ node test/runner.js
/Users/dc/dev/exiteer/xbot/server/src/mup/Story.js:24
Logger.logObj('loaded story', {name: doc.name})
^
ReferenceError: doc is not defined
at Story.reload (/Users/dc/dev/exiteer/xbot/server/src/mup/Story.js:24:42)
at Game.reload (/Users/dc/dev/exiteer/xbot/server/src/mup/Game.js:48:16)
at Object.<anonymous> (/Users/dc/dev/exiteer/xbot/server/test/runner.js:4:10)
Sweet, I can fix it.
Now, Jest has some nice tooling for writing tests so I thought I'd try that.
But the errors are seemingly impossible to track down:
➜ server git:(dc/build) ✗ npm run jest
> cbg#0.1.0 jest /Users/dc/dev/exiteer/xbot/server
> jest
PASS src/index.test.js
(node:23114) UnhandledPromiseRejectionWarning: ReferenceError: doc is not defined
(Use `node --trace-warnings ...` to show where the warning was created)
(node:23114) 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: 1)
(node:23114) [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.
FAIL src/mup/Actor.test.js
● Console
console.log
actors undefined
at Object.<anonymous> (src/mup/Actor.test.js:9:13)
● Game.js › can load a story
expect(received).toHaveLength(expected)
Matcher error: received value must have a length property whose value must be a number
Received has value: undefined
8 |
9 | console.log('actors', game.story.room.name.actors)
> 10 | expect(game.story.room.actors).toHaveLength(1);
| ^
11 | const actor = game.story.room.actors[0]
12 | const reply = actor.sayTo('hi')
13 | expect(reply).toBe('hi back from Sid')
at Object.<anonymous> (src/mup/Actor.test.js:10:36)
This tells me where my tests failed, but I'd prefer to know where the actual error is. Tests aren't the end goal here, a working app is.
Googling around I found and tried this but it gives the same error message.
node --trace-warnings node_modules/.bin/jest --no-cache
➜ server git:(dc/build) ✗ npm run test
> cbg#0.1.0 test /Users/dc/dev/exiteer/xbot/server
> node --trace-warnings node_modules/.bin/jest --no-cache
(node:23263) UnhandledPromiseRejectionWarning: ReferenceError: doc is not defined
at emitUnhandledRejectionWarning (internal/process/promises.js:151:15)
at processPromiseRejections (internal/process/promises.js:211:11)
at processTicksAndRejections (internal/process/task_queues.js:98:32)
(node:23263) 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: 1)
which gives a tiny bit more info but an
at emitUnhandledRejectionWarning (internal/process/promises.js:151:15)
at processPromiseRejections (internal/process/promises.js:211:11)
at processTicksAndRejections (internal/process/task_queues.js:98:32)
This is not very helpful for debugging my code.
Also Jest seems to swallow all console.log as if it's doing the best to make debugging as painful as possible. When your environment doesn't even log, its a real WTF moment.
This was a bug in Node.js or Jest depending on your perspective - I fixed this a few months ago.
It happens because Jest throws errors in JavaScript coming from a different realm - so instanceof Error fails. You can see my PR here.
There is a workaround if you are forced to use old versions of Node or Jest here:
process.on('unhandledRejection', (reason) => {
console.log(reason); // log the reason including the stack trace
throw e;
});
We added this hook in Node 1.3 so it should be safe to use in virtually all Node code.

react native yarn start using expo is not working

I'm trying to run the expo but nothing happens.
My try:
➜ expoTest git:(master) ✗ yarn start
yarn run v1.22.4
$ expo start
Starting project at /Users/seungjune/project/tutorial/expoTest
When I tried npm start or yarn start or expo-cli start, it still not working after the message as follows
Starting project at /Users/seungjune/project/tutorial/expoTest
I found error after starting meesage as follows
(node:3581) UnhandledPromiseRejectionWarning: RangeError [ERR_SOCKET_BAD_PORT] [ERR_SOCKET_BAD_PORT]: options.port should be >= 0 and < 65536. Received 65536.
at validatePort (internal/validators.js:193:11)
at Server.listen (net.js:1440:5)
at /usr/local/lib/node_modules/expo-cli/node_modules/freeport-async/index.js:8:12
at new Promise (<anonymous>)
at testPortAsync (/usr/local/lib/node_modules/expo-cli/node_modules/freeport-async/index.js:6:10)
at availableAsync (/usr/local/lib/node_modules/expo-cli/node_modules/freeport-async/index.js:24:17)
at /usr/local/lib/node_modules/expo-cli/node_modules/freeport-async/index.js:37:23
at new Promise (<anonymous>)
at freePortRangeAsync (/usr/local/lib/node_modules/expo-cli/node_modules/freeport-async/index.js:33:10)
at /usr/local/lib/node_modules/expo-cli/node_modules/freeport-async/index.js:43:18
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3581) 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)
(node:3581) [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.

Cant host discord bot (js) on heroku

I am trying to host a bot on heroku (written using node and discord js). The bot itself is working since I can run it locally with no issues but heroku doesn't seem to like it. I've been getting the same error every time I tried to deploy it. I've changed the Procfile file to a worker instead of web application, I have pushed everything + restarted but can't seem to get it to work. Any help?
2020-01-28T11:31:46.007810+00:00 heroku[worker.1]: State changed from crashed to starting
2020-01-28T11:31:48.995011+00:00 heroku[worker.1]: Starting process with command `node index.js`
2020-01-28T11:31:49.818228+00:00 heroku[worker.1]: State changed from starting to up
2020-01-28T11:31:52.030152+00:00 heroku[worker.1]: State changed from up to crashed
2020-01-28T11:31:52.006456+00:00 heroku[worker.1]: Process exited with status 0
2020-01-28T11:31:51.886594+00:00 app[worker.1]: (node:4) UnhandledPromiseRejectionWarning: Error: An invalid token was provided.
2020-01-28T11:31:51.886618+00:00 app[worker.1]: at /app/node_modules/discord.js/src/client/rest/RESTMethods.js:34:54
2020-01-28T11:31:51.886621+00:00 app[worker.1]: at new Promise (<anonymous>)
2020-01-28T11:31:51.886623+00:00 app[worker.1]: at RESTMethods.login (/app/node_modules/discord.js/src/client/rest/RESTMethods.js:33:12)
2020-01-28T11:31:51.886625+00:00 app[worker.1]: at Client.login (/app/node_modules/discord.js/src/client/Client.js:280:30)
2020-01-28T11:31:51.886627+00:00 app[worker.1]: at Object.<anonymous> (/app/index.js:45:8)
2020-01-28T11:31:51.886629+00:00 app[worker.1]: at Module._compile (internal/modules/cjs/loader.js:955:30)
2020-01-28T11:31:51.886631+00:00 app[worker.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
2020-01-28T11:31:51.886633+00:00 app[worker.1]: at Module.load (internal/modules/cjs/loader.js:811:32)
2020-01-28T11:31:51.886635+00:00 app[worker.1]: at Function.Module._load (internal/modules/cjs/loader.js:723:14)
2020-01-28T11:31:51.886637+00:00 app[worker.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
2020-01-28T11:31:51.887556+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(). (rejection id: 3)
2020-01-28T11:31:51.887666+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.
With just the error it is hard to know the exact problem, but I think that you might have given a wrong Token for the bot, check if you didn't ignore the file containing the token.
If you are using a .env file for your token, then you can define environment variables on the app on Heroku.

MongoDB working on local server.js but saying error on heroku

on deploying meanapp , mongodb is working fine on local node server.js but when i deploy to heroku it showing error
2019-06-04T02:49:12.401001+00:00 heroku[web.1]: State changed from up
to starting
2019-06-04T02:49:13.706279+00:00 heroku[web.1]: Stopping all processes
with SIGTERM
2019-06-04T02:49:13.800926+00:00 heroku[web.1]: Process exited with
status 143
2019-06-04T02:49:17.993509+00:00 heroku[web.1]: Starting process with
command node server.js
2019-06-04T02:49:20.786431+00:00 app[web.1]:
mongodb+srv://username:password#cluster0-dlcak.mongodb.net
/test?retryWrites=true 2019-06-04T02:49:20.803912+00:00 app[web.1]:
app listening at 40970
2019-06-04T02:49:20.972565+00:00 app[web.1]: database error
MongoNetworkError: connection 4 to
cluster0-shard-00-01-dlcak.mongodb.net:27017 closed
2019-06-04T02:49:20.973723+00:00 app[web.1]: (node:4)
UnhandledPromiseRejectionWarning: TypeError: callback is not a
function
2019-06-04T02:49:20.973725+00:00 app[web.1]: at
$initialConnection.$initialConnection.then
(/app/node_modules/mongoose/lib/connection.js:638:13)
2019-06-04T02:49:20.973810+00:00 app[web.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(). (rejection id: 1)
2019-06-04T02:49:20.973880+00:00 app[web.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.
2019-06-04T02:49:21.758205+00:00 heroku[web.1]: State changed from
starting to up
I got the solution while searching,
go to mongodb atlas
click security -> Network Access -> ADD IP ADDRESS -> Choose allow access from anywhere and save
click connect and get your application to your code and paste to your application.
How is it configured on Heroku?
Use
https://elements.heroku.com/addons/mongolab -- for reference
Add
const mongo_url = process.env.MONGO_URI || "YOUR_URL_HERE";
to your code

Resources