Angular SSR项目部署至cPanel Node.js环境遭遇503错误及UnhandledPromiseRejectionWarning问题求助
Looks like you're hitting compatibility hurdles deploying your Angular 21 SSR app on cPanel with LiteSpeed. Let's break down the issues you're seeing and walk through actionable fixes step by step:
First: Diagnose the "Error: Not supported" from lsnodesm.js
This error originates from LiteSpeed's Node.js handler script, which usually points to a mismatch between your app's module system (ES Modules, since your package.json has "type": "module") and how LiteSpeed tries to run your app. Here's how to resolve this:
- Verify your startup file path in cPanel: Double-check that the "Application startup file" setting points to the correct
server.mjsgenerated by Angular's SSR build. For most Angular 21 projects, this will be indist/server/server.mjs(not a root-level file unless you explicitly moved it). - Test manual startup first: Bypass LiteSpeed's auto-handler temporarily by running the app directly in cPanel's terminal:
This gives you raw, unfiltered error logs that might reveal issues cPanel's handler is hiding.node dist/server/server.mjs - Add explicit error handling to your server script: The unhandled promise rejections mean your server is crashing silently before LiteSpeed can proxy to it. Update your
server.mjswith error catching to get detailed logs:
This will force the server to log exact errors instead of just crashing with "Not supported".import { APP_BASE_HREF } from '@angular/common'; import { CommonEngine } from '@angular/ssr'; import express from 'express'; import { fileURLToPath } from 'node:url'; import { dirname, join, resolve } from 'node:path'; import AppServerModule from './src/main.server'; async function run() { try { const app = express(); const __dirname = dirname(fileURLToPath(import.meta.url)); const commonEngine = new CommonEngine(); app.set('view engine', 'html'); app.set('views', join(__dirname, '../browser')); app.get('*.*', express.static(resolve(__dirname, '../browser'), { maxAge: '1y' })); app.get('*', (req, res, next) => { const { protocol, originalUrl, baseUrl, headers } = req; commonEngine .render({ bootstrap: AppServerModule, documentFilePath: join(__dirname, '../browser/index.html'), url: `${protocol}://${headers.host}${originalUrl}`, publicPath: join(__dirname, '../browser'), providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }], }) .then((html) => res.send(html)) .catch((err) => next(err)); }); const port = process.env.PORT || 4000; app.listen(port, () => { console.log(`Server listening on port ${port}`); }); } catch (err) { console.error('Fatal error starting server:', err); process.exit(1); } } // Catch unhandled errors for debugging process.on('unhandledRejection', (reason, promise) => { console.error('Unhandled Rejection at:', promise, '\nReason:', reason); }); process.on('uncaughtException', (err) => { console.error('Uncaught Exception:', err); process.exit(1); }); run();
Second: Fix the "Unexpected token '??='" Syntax Error
The nullish coalescing assignment operator (??=) is ES2020 syntax, which Node.js 24+ supports natively. This error usually stems from one of two issues:
- Your server's Node.js version isn't actually 24.13.0: Run
node -vin cPanel's terminal to confirm. Sometimes cPanel's Node.js selector doesn't apply correctly to the terminal or LiteSpeed process. If it's lower, reconfigure cPanel to use 24.13.0 or higher. - A dependency uses untranspiled ES2020+ code: Check your
package.jsonoverrides and dependencies. Yourundicioverride to^7.24.0is compatible with Node.js 24, but outdated dependencies might cause conflicts. Runnpm lsin the server terminal to spot problematic packages.
Third: Validate Your Angular Build & Deployment Flow
Ensure you're following the correct SSR deployment steps:
- Build the app locally with the SSR target:
npm run build:ssr - Upload only necessary files to cPanel:
dist/folder (contains both browser and server builds)package.jsonpackage-lock.json(ornpm-shrinkwrap.json)
- Install production dependencies on the server:
npm install --production - Check file permissions: cPanel environments typically require
755for folders and644for files. Runls -lin the terminal to verify permissions, and adjust if needed.
Final Fallback: Switch to CommonJS Modules
If ES Modules still cause issues (some LiteSpeed versions have better CommonJS support):
- Remove
"type": "module"frompackage.json - Rename
server.mjstoserver.js - Update
server.jsto use CommonJS syntax:const { APP_BASE_HREF } = require('@angular/common'); const { CommonEngine } = require('@angular/ssr'); const express = require('express'); const { join, resolve } = require('path'); const AppServerModule = require('./src/main.server').default; async function run() { // ... rest of the server logic using require/module.exports instead of import/export }
内容的提问来源于stack exchange,提问作者metal-draken

