Skip to content

Angular SSR server.mjs  #5921

Closed
Closed

Description

@akilah007

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

Activity

  1. MehdiBenfredj commented on Feb 3, 2025

    @MehdiBenfredj

    Hello, did you find any solution?

  2. MehdiBenfredj commented on Feb 3, 2025

    @MehdiBenfredj
  3. akilah007 commented on Feb 4, 2025

    @akilah007
    Author
  4. MehdiBenfredj commented on Feb 4, 2025

    @MehdiBenfredj

    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.

  5. akilah007 commented on Feb 5, 2025

    @akilah007
    Author
  6. niceilm commented on Mar 13, 2025

    @niceilm

    Thanks a lot. it worked!

  7. raouf-b-dev commented on May 10, 2025

    @raouf-b-dev

    Thank you so much, even AI chatbots did not find the problem. Thank you very much.
    And it's even well explained.

    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.

  8. ManNQ-GSV commented on Aug 7, 2025

    @ManNQ-GSV

    Beautiful, many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions