Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular SSR server.mjs #5921

Closed
akilah007 opened this issue Nov 24, 2024 · 6 comments
Closed

Angular SSR server.mjs #5921

akilah007 opened this issue Nov 24, 2024 · 6 comments

Comments

@akilah007
Copy link

App, not running

pm2 start server.mjs success and shows online, but app not running on the port 4000

Supporting information

Angular Version 19.0.0
pm2 version 5.4.3
nodejs version 20.16.0

# Run the following commands
$ pm2 report
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 5.4.3
node version         : 20.16.0
node path            : /usr/local/bin/pm2
argv                 : /usr/bin/node,/usr/local/lib/node_modules/pm2/lib/Daemon.js
argv0                : node
user                 : root
uid                  : 0
gid                  : 0
uptime               : 34min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 5.4.3
node version         : 20.16.0
node path            : /usr/local/bin/pm2
argv                 : /usr/bin/node,/usr/local/bin/pm2,report
argv0                : node
user                 : root
uid                  : 0
gid                  : 0
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : linux
type                 : Linux
cpus                 : DO-Regular
cpus nb              : 1
freemem              : 179175424
totalmem             : 479989760
home                 : /root
===============================================================================
PM2        | 2024-11-24T15:02:34: PM2 log: App [api:1] online
PM2        | 2024-11-24T15:08:06: PM2 log: Stopping app:server id:0
PM2        | 2024-11-24T15:08:06: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
PM2        | 2024-11-24T15:08:06: PM2 log: pid=5503 msg=process killed
PM2        | 2024-11-24T15:08:49: PM2 log: App [server:2] starting in -fork mode-
PM2        | 2024-11-24T15:08:50: PM2 log: App [server:2] online
PM2        | 2024-11-24T15:12:54: PM2 log: Stopping app:server id:2
PM2        | 2024-11-24T15:12:54: PM2 log: App [server:2] exited with code [0] via signal [SIGINT]
PM2        | 2024-11-24T15:12:54: PM2 log: pid=5760 msg=process killed
PM2        | 2024-11-24T15:12:54: PM2 log: App [server:2] starting in -fork mode-
PM2        | 2024-11-24T15:12:54: PM2 log: App [server:2] online
PM2        | 2024-11-24T15:14:32: PM2 log: Stopping app:server id:2
PM2        | 2024-11-24T15:14:32: PM2 log: App [server:2] exited with code [0] via signal [SIGINT]
PM2        | 2024-11-24T15:14:32: PM2 log: pid=5845 msg=process killed
PM2        | 2024-11-24T15:15:12: PM2 log: App [server:3] starting in -cluster mode-
PM2        | 2024-11-24T15:15:12: PM2 log: App [server:3] online
PM2        | 2024-11-24T15:17:15: PM2 log: Stopping app:server id:3
PM2        | 2024-11-24T15:17:15: PM2 log: App name:server id:3 disconnected
PM2        | 2024-11-24T15:17:15: PM2 log: App [server:3] exited with code [0] via signal [SIGINT]
PM2        | 2024-11-24T15:17:16: PM2 log: pid=5964 msg=process killed
@MehdiBenfredj
Copy link

Hello, did you find any solution?

@MehdiBenfredj
Copy link

Hi,

This should work ( it worked for me )
https://stackoverflow.com/questions/79214195/is-angular-19-0-incompatible-with-pm2

@akilah007
Copy link
Author

akilah007 commented Feb 4, 2025 via email

@MehdiBenfredj
Copy link

before:

/**
 * 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}`);
  });
}

after:

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

explanation:

"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."

What you have to do instead is add a a another condition, that checks that is it being called by pm2 through an env variable. by adding the var to the ecosystem.config.js

Next necessary steps:

-> create ecosystm.config.js:

 module.exports = {
        apps: [{
          name: 'app_name',
          script: 'server.mjs', // Replace with your entry file
          env: {
                PM2: "true"
                ... // rest of your vars
          }
          .... // rest of your config
        }]
  };

-> make sure to launch from ecosystem.config.js : pm2 start ecosystem.config.js
this starts the server with the right env variables.

@akilah007
Copy link
Author

akilah007 commented Feb 5, 2025 via email

@niceilm
Copy link

niceilm commented Mar 13, 2025

Thanks a lot. it worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants