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 |2 Answers
Reset to default 2I'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:
- 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();
}
- Create/update your PM2 ecosystem file:
module.exports = {
apps: [{
name: "angular-app",
script: "dist/server/server.mjs",
env: {
PM2: "true"
}
}]
}
- 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
版权声明:本文标题:server side rendering - Is Angular 19.0 incompatible with pm2? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736305130a1932482.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
^20.11.1
or^22.0.0
? See angular.dev/reference/versions – JSON Derulo Commented Nov 22, 2024 at 9:34pm2-dev start "npm start"
for make sure pm2 debug is run – emild_ Commented Nov 23, 2024 at 14:00