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

Angular SSR项目报错:No NgModule metadata found for 'AppServerModule'求助

Fixing "No NgModule metadata found for 'AppServerModule'" in Angular SSR Projects

Hey there, let's tackle this frustrating error you're hitting with your Angular SSR project. It's a common gotcha, so we'll walk through the most likely fixes step by step based on the code you shared.

1. Double-Check File Naming & Paths

First off, let's rule out the simplest mistake: file extensions and path correctness.

  • From your code snippet, you referenced the file as src/app/app.server.module—but Angular expects module files to have the .ts extension. Make sure your file is actually named app.server.module.ts (not missing the .ts suffix).
  • Verify the import path in main.server.ts:
    export { AppServerModule } from './app/app.server.module';
    
    If your file is named correctly with .ts, this path is valid. But if the filename has a typo (like app.server.modules.ts), that would break the import entirely.

2. Validate Angular CLI SSR Configuration

If you set up SSR via the Angular CLI (ng add @nguniversal/express-engine), confirm your project config is pointing to the right files:

  • Open angular.json and check the server build target. It should reference src/main.server.ts as the entry point, and use tsconfig.server.json for TypeScript settings.
  • Open tsconfig.server.json:
    • Ensure the files array includes src/main.server.ts
    • Make sure the include array covers your app files (e.g., "src/app/**/*.ts") so AppServerModule gets included in the server build.

3. Confirm AppServerModule's NgModule Decorator

Your AppServerModule code looks correct at first glance, but let's double-check for tiny typos that can break things:

  • Ensure the @NgModule decorator is spelled correctly (capital M in NgModule—it's easy to accidentally type Ngmodule).
  • Verify the decorator's parentheses are properly paired, and all imports/bootstraps are correctly listed inside the object.

4. Clean Caches & Rebuild From Scratch

Sometimes stale caches or corrupted dependencies cause weird issues. Try these steps:

  • Run ng clean to clear Angular's build cache
  • Delete node_modules and package-lock.json (or yarn.lock if you use Yarn)
  • Reinstall dependencies: npm install (or yarn install)
  • Rebuild and serve the SSR project:
    npm run build:ssr
    npm run serve:ssr
    

5. Check TypeScript Compiler Options

Make sure tsconfig.server.json isn't excluding your server module:

  • Check the exclude array—don't let it include src/app/app.server.module.ts
  • Confirm the module option is set to commonjs (Node.js requires CommonJS module format for SSR, which is the Angular default, but it might have been changed accidentally).

Start with the file extension/path check—those are the most frequent culprits here. If that doesn't fix it, work through the other steps one by one. Let me know if any of these resolve your issue!

内容的提问来源于stack exchange,提问作者Travis Knight

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 15:12:27