You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular SSR项目部署至cPanel Node.js环境遭遇503错误及UnhandledPromiseRejectionWarning问题求助

Troubleshooting Angular 21 SSR Deployment on cPanel (503 Error & Syntax Issues)

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.mjs generated by Angular's SSR build. For most Angular 21 projects, this will be in dist/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:
    node dist/server/server.mjs
    
    This gives you raw, unfiltered error logs that might reveal issues cPanel's handler is hiding.
  • 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.mjs with error catching to get detailed logs:
    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();
    
    This will force the server to log exact errors instead of just crashing with "Not supported".

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:

  1. Your server's Node.js version isn't actually 24.13.0: Run node -v in 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.
  2. A dependency uses untranspiled ES2020+ code: Check your package.json overrides and dependencies. Your undici override to ^7.24.0 is compatible with Node.js 24, but outdated dependencies might cause conflicts. Run npm ls in the server terminal to spot problematic packages.

Third: Validate Your Angular Build & Deployment Flow

Ensure you're following the correct SSR deployment steps:

  1. Build the app locally with the SSR target:
    npm run build:ssr
    
  2. Upload only necessary files to cPanel:
    • dist/ folder (contains both browser and server builds)
    • package.json
    • package-lock.json (or npm-shrinkwrap.json)
  3. Install production dependencies on the server:
    npm install --production
    
  4. Check file permissions: cPanel environments typically require 755 for folders and 644 for files. Run ls -l in 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" from package.json
  • Rename server.mjs to server.js
  • Update server.js to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 06:42:43