-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Comments
Hello, did you find any solution? |
Hi, This should work ( it worked for me ) |
no I used, forever instead of pm2
…On Tue, Feb 4, 2025 at 12:25 AM Mehdi BENFREDJ ***@***.***> wrote:
Hello, did you find any solution?
—
Reply to this email directly, view it on GitHub
<#5921 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABRLCTEVXD57AIQQXFAIDY32N63SDAVCNFSM6AAAAABSMLGZYKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMMZRHAYDMOJXGM>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
before:
after:
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 Next necessary steps: -> create
-> make sure to launch from |
Thanks a lot. it worked!
…On Wed, Feb 5, 2025 at 1:34 AM Mehdi BENFREDJ ***@***.***> wrote:
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.
—
Reply to this email directly, view it on GitHub
<#5921 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABRLCTCB2TNJOZNSP4UQACD2OEMN5AVCNFSM6AAAAABSMLGZYKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMMZUHE2DOMJRHE>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Thanks a lot. it worked! |
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
The text was updated successfully, but these errors were encountered: