I can't get pm2 to work with a fresh Angular 19.0 build.
Here's what I’ve tried:
ng new app
ng build
pm2 start dist/server/server.mjs
The node server is not listening on any port. The process appears online in pm2, but it doesn't log anything.
When I launch the server directly using node dist/server/server.mjs
, the server starts up and runs as expected.
If I downgrade to angular 18.2, the server runs as usual with pm2.
Environment:
I have no clue what's happening since the logs are empty. How can I debug this?
I'll answer my own question as I figured it out.
In the default generated server.ts
file when creating a new app, the following lines appear at the end of the file:
/**
* Start the server if this module is the main entry point.
* The server listens on the port defined by the `PORT` environment variable, or defaults to 4000.
*/
if (isMainModule(import.meta.url)) {
const port = process.env['PORT'] || 4000;
app.listen(port, () => {
console.log(`Node Express server listening on http://localhost:${port}`);
});
}
The isMainModule
function comes from the @angular/ssr
package and "Determines whether the provided URL represents the main entry point module."
However, PM2 uses a container around the Node process to manage it (see pm2/lib/ProcessContainerFork.js
). As a result, the function failed to determine that it's the main module who call it, the condition is never met, and the process stops there.