Search code examples
javascriptnode.jspromiseunhandled

Can I get the future unhandled promise rejection behaviour now?


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?


Solution

  • 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)
    });