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?

Erik B
  • 40,889
  • 25
  • 119
  • 135

4 Answers4

15

For node 12 and later:

node --unhandled-rejections=strict
No_name
  • 2,732
  • 3
  • 32
  • 48
  • I am actually stuck,where to run this command. My build is keep giving error Like this (node:23280) UnhandledPromiseRejectionWarning: Error: NGCC failed. please help – ammad khan Sep 29 '22 at 07:45
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)
});
Thomas
  • 174,939
  • 50
  • 355
  • 478
  • 1
    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. – Erik B Aug 01 '18 at 13:05
  • 2
    You can put this in a file (say `handle_promise_rejections.js`) and use `node -r ./handle_promise_rejections.js ` to preload it. – Thomas Aug 01 '18 at 13:17
  • Beautiful - thanks for your answer – Joshua Wolff Dec 02 '21 at 03:11
  • I am actually stuck,where to run this command. My build is keep giving error Like this (node:23280) UnhandledPromiseRejectionWarning: Error: NGCC failed. please help – ammad khan Sep 29 '22 at 07:45
  • This answer is probably not a solution to your problem @ammadkhan. – Thomas Oct 03 '22 at 12:38
12

I like setting these options via the environment variable:

export NODE_OPTIONS="--unhandled-rejections=strict"
SgtPooki
  • 11,012
  • 5
  • 37
  • 46
  • where i can do this ? in windows environment variable or environment.ts file? – ammad khan Sep 29 '22 at 08:49
  • 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 https://www.rapidee.com/ for isolated cases – SgtPooki Oct 03 '22 at 15:31
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.

Oliver Groß
  • 121
  • 1
  • 5
  • I am actually stuck,where to run this command. My build is keep giving error Like this (node:23280) UnhandledPromiseRejectionWarning: Error: NGCC failed. please help – ammad khan Sep 29 '22 at 07:45