0

I can't get why a simple node process failing doesn't get detected by pm2.

pm2-dev app.js (or pm2 start app.js) with app.js containing :

throw new Error("test");

=> pm2 does not detect any error and consider the pm2 as "active" when I pm2 status or pm2 list

import "non-existing-file.js";

=> pm2 does not detect any error and consider the pm2 as "active" when I pm2 status or pm2 list

process.exit(1);

=> pm2 detect the error and consider the pm2 in error

Is it normal that the first 2 cases are not detected ? I tried playing with uncaughtException handler but of course failed ...

The goal : I don't want pm2 to consider my app as "alive" if my deployment somehow outputed a broken build (with js import errors for example)

Thanks

2
  • throw new Error("test") - this is not reproducible, process status is "stopped" or "errored". import "non-existing-file.js" - this is reproducible, could be specific to node module loader, but then the existing answer applies, you can use a single try...catch on import('app.js') for the whole app Commented May 3 at 10:36
  • @EstusFlask problem was --unhandled-rejections node option being weirdly set on windows and linux, but not on mac ... Commented May 5 at 19:34

2 Answers 2

1

Use a startup error trap like this:

process.on("uncaughtException", (err) => {
  console.error("❌ Startup error caught:", err);
  process.exit(1); // PM2 will detect this as a failure
});

throw new Error("test"); // Now this will trigger process.exit(1)

For import errors:

Use dynamic import inside a try-catch:

(async () => {
  try {
    await import('./non-existing-file.js');
  } catch (err) {
    console.error("❌ Import failed:", err);
    process.exit(1); // PM2 will catch this now
  }
})();

Sign up to request clarification or add additional context in comments.

3 Comments

1st don't work with pm2, 2nd works .. But I don't want to jungle with all nodejs possible errors.. I want a once-and-for-all solution. Seems like pm2 is bugged (since 2021 ? github.com/Unitech/pm2/issues/5013) because my 3 examples are all returning "1" as "return code" : node app.js ; echo $? So pm2 should handle all these cases as error ...
@Jscti It's unclear what answer is expected if it's a known issue and you're aware of it. You need to handle uncaughtException and unhandledRejection to not rely on pm2's unspecified and probably faulty behaviour, not "all nodejs possible errors"
@EstusFlask I'm obvisouly handling nodejs errors in my app. My question was more global about how pm2 handles script exit codes. II had a build error leading to a totally broken js code that was not detected as error by pm2. Found the solution now
1

After re-trying the same 3 files on a Mac : it worked as intended : pm2 goes in error + restarts .. It was only on Windows and Linux, that the pm2 behavior was different (tested 3 OS with same nodejs versions) and erroneous

Then I found it : node option --unhandled-rejections :

pm2-dev .\app.js --node-args="--unhandled-rejections=strict"

pm2 now detect error exit codes like it should.

3 Comments

Interesting. The option could propagate from NODE_OPTIONS env var. Does it not work as intended with --unhandled-rejections=throw ?
@EstusFlask throw does not work, only "strict". But after digging some more, I found something else : my test.js file was in a folder with a package.js containing {"type":"module"} .. If I remove this, the error propagates normally ...
As I mentioned, this could be something specific to node esm loader. I saw that pm2 hooks into node cjs loader for its own purposes. I didn't check how it deals with esm but apparently it's different, probably there's a bug that needs to be reported. For this reason a hack suggested in another answer could help. Any way, I appreciate the feedback, this looks like a tricky pitfall I'll keep in mind

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.