admin管理员组

文章数量:1122833

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:

  • node 20.9.0
  • pm2 5.3.1
  • angular 19.0.0

I have no clue what's happening since the logs are empty. How can I debug this?

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:

  • node 20.9.0
  • pm2 5.3.1
  • angular 19.0.0

I have no clue what's happening since the logs are empty. How can I debug this?

Share Improve this question asked Nov 22, 2024 at 8:30 Lucas TesseronLucas Tesseron 4154 silver badges11 bronze badges 4
  • Node 20.9.0 is not supported by Angular 19, could you try updating to ^20.11.1 or ^22.0.0? See angular.dev/reference/versions – JSON Derulo Commented Nov 22, 2024 at 9:34
  • Good point thanks. I just tried node 20.11.1 with pm2 5.4.3 but it doesn't work either – Lucas Tesseron Commented Nov 22, 2024 at 9:45
  • did you serve angular as static site? maybe pm2-dev start "npm start" for make sure pm2 debug is run – emild_ Commented Nov 23, 2024 at 14:00
  • Thanks for your suggestion. Running the start command works as expected: the app is first built then served (logs appear, but there are no errors). – Lucas Tesseron Commented Nov 25, 2024 at 9:25
Add a comment  | 

2 Answers 2

Reset to default 2

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.

I found a solution to this problem. It seems Angular 19's server.mjs module structure requires explicit handling for PM2 environments.

The issue occurs because the server only starts when it's the main module, but PM2 changes this behavior. Here's how to fix it:

  1. Modify your server.mjs:
export function startServer() {
  const port = process.env['PORT'] || 4000;
  app.listen(port, () => {
    console.log(`Node Express server listening on http://localhost:${port}`);
  });
}

const metaUrl = import.meta.url;
const isMain = isMainModule(metaUrl);
const isPM2 = process.env.PM2 === 'true';

if (isMain || isPM2) {
  startServer();
}
  1. Create/update your PM2 ecosystem file:
module.exports = {
  apps: [{
    name: "angular-app",
    script: "dist/server/server.mjs",
    env: {
      PM2: "true"
    }
  }]
}
  1. Start with: pm2 start ecosystem.config.js

This works because it explicitly checks for both the main module condition and a PM2 environment flag. The server will now start correctly under PM2.

Hope this helps someone else running into the same issue with Angular 19!

本文标签: server side renderingIs Angular 190 incompatible with pm2Stack Overflow