Can I get the future unhandled promise rejection behaviour now?
Asked Answered
L

4

11

In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

My pipeline passed when it should have failed and deployed a version that is crashing on launch. If Node.js would have exited with a non-zero exit code, the pipeline would have failed and the bad version wouldn't have been deployed.

Is there a way to make Node.js exit with a non-zero exit code when it encounters an unhandled promise rejection, that doesn't require me to wait for the future?

Ligation answered 1/8, 2018 at 12:9 Comment(1)
Look at this: medium.com/dailyjs/…Bookerbookie
T
12

Yes, you can, using the unhandledRejection event on the process object:

process.on('unhandledRejection', (reason, p) => {
  console.error('Unhandled Rejection at:', p, 'reason:', reason)
  process.exit(1)
});
Treadwell answered 1/8, 2018 at 12:15 Comment(5)
Each part of the pipeline has a different entry point, so I would have to add this everywhere. The risk of forgetting this is the same as the risk of forgetting to put a catch block, which was the original problem. I was hoping for a flag, like --exit-on-unhandled-promise-rejections or even --exit-on-warnings, but I'm not finding anything like that. I would still have to remember to add the flag, but since that is done in one place (package.json), it would be much easier to remember.Ligation
You can put this in a file (say handle_promise_rejections.js) and use node -r ./handle_promise_rejections.js <your_script.js> to preload it.Treadwell
Beautiful - thanks for your answerBabe
I am actually stuck,where to run this command. My build is keep giving error Like this (node:23280) UnhandledPromiseRejectionWarning: Error: NGCC failed. please helpMend
This answer is probably not a solution to your problem @ammadkhan.Treadwell
J
13

For node 12 and later:

node --unhandled-rejections=strict
Jethro answered 22/8, 2019 at 16:2 Comment(1)
I am actually stuck,where to run this command. My build is keep giving error Like this (node:23280) UnhandledPromiseRejectionWarning: Error: NGCC failed. please helpMend
T
12

Yes, you can, using the unhandledRejection event on the process object:

process.on('unhandledRejection', (reason, p) => {
  console.error('Unhandled Rejection at:', p, 'reason:', reason)
  process.exit(1)
});
Treadwell answered 1/8, 2018 at 12:15 Comment(5)
Each part of the pipeline has a different entry point, so I would have to add this everywhere. The risk of forgetting this is the same as the risk of forgetting to put a catch block, which was the original problem. I was hoping for a flag, like --exit-on-unhandled-promise-rejections or even --exit-on-warnings, but I'm not finding anything like that. I would still have to remember to add the flag, but since that is done in one place (package.json), it would be much easier to remember.Ligation
You can put this in a file (say handle_promise_rejections.js) and use node -r ./handle_promise_rejections.js <your_script.js> to preload it.Treadwell
Beautiful - thanks for your answerBabe
I am actually stuck,where to run this command. My build is keep giving error Like this (node:23280) UnhandledPromiseRejectionWarning: Error: NGCC failed. please helpMend
This answer is probably not a solution to your problem @ammadkhan.Treadwell
W
12

I like setting these options via the environment variable:

export NODE_OPTIONS="--unhandled-rejections=strict"
Walkover answered 9/12, 2020 at 22:59 Comment(2)
where i can do this ? in windows environment variable or environment.ts file?Mend
It's been a while since I've worked on windows but an .env file will be a universal method of adding this to a project. You can also use rapidee.com for isolated casesWalkover
F
6

You may also add an .npmrc file in your projects root directory. Place the following content into the file:

node-options="--unhandled-rejections=strict"

Each time you execute an npm command the directories are parsed for the file and options from the file are read.

You may run one of your npm run defined in your package.json.

./my-project-folder
     |-- .npmrc
     |-- package.json
     |-- src/
           |-- index.js

Change to your working directory and execute the command

npm run start
// or
npm test

"start" and "test" are scripts in your package.json that needs to be defined. npm used node to execute your code and the node binary itself is looking for .npmrc files to configure its runtime.

Fencesitter answered 1/9, 2021 at 12:47 Comment(1)
I am actually stuck,where to run this command. My build is keep giving error Like this (node:23280) UnhandledPromiseRejectionWarning: Error: NGCC failed. please helpMend

© 2022 - 2024 — McMap. All rights reserved.