Angular Universal收到“找不到'[object Object]'的NgModule元数据”错误

时间:2019-01-18 09:49:43

标签: angular angular-universal ssr

我刚刚将Angular应用程序从5更新到了7。在第5版中,我能够很好地完成服务器端渲染(SSR)。

更新后,SSR失败,并显示以下错误(构建过程正常):

Error: No NgModule metadata found for '[object Object]'.
at NgModuleResolver.resolve (/app/node_modules/@angular/compiler/bundles/compiler.umd.js:20015:27)
at CompileMetadataResolver.getNgModuleMetadata (/app/node_modules/@angular/compiler/bundles/compiler.umd.js:18657:47)
at JitCompiler._loadModules (/app/node_modules/@angular/compiler/bundles/compiler.umd.js:26060:55)
at JitCompiler._compileModuleAndComponents (/app/node_modules/@angular/compiler/bundles/compiler.umd.js:26041:40)
at JitCompiler.compileModuleAsync (/app/node_modules/@angular/compiler/bundles/compiler.umd.js:26001:41)
at CompilerImpl.compileModuleAsync (/app/node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js:202:35)
at /app/node_modules/@nguniversal/express-engine/bundles/express-engine.umd.js:109:25
at new ZoneAwarePromise (/app/node_modules/zone.js/dist/zone-node.js:910:29)
at getFactory (/app/node_modules/@nguniversal/express-engine/bundles/express-engine.umd.js:95:12)
at View.engine (/app/node_modules/@nguniversal/express-engine/bundles/express-engine.umd.js:74:13)

鉴于此SO question,我没有使用"bundleDependencies": "all",我什至尝试在none中明确声明angular.json,但并不能解决问题。< / p>

我正在angular.json中使用此配置:

   "server": {
      "builder": "@angular-devkit/build-angular:server",
      "options": {
        "outputPath": "dist/server",
        "main": "src/main.server.ts",
        "tsConfig": "src/tsconfig.server.json",
        "bundleDependencies": "none"
      },
      "configurations": {
        "production": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.prod.ts"
            }
          ],
          "outputHashing": "none"
        }
      }
    }

如果这很重要,我也很懒惰地加载几乎所有模块(在第5节中没有):

{ path: 'private', loadChildren: '../private/private.module#PrivateModule', canActivate: [AuthGuardService], data: { state: 'private' } as RouteDataValues },
{
  path: '', component: PageWithFooterComponent, children: [
    { path: 'about', loadChildren: '../about/about.module#AboutModule', data: { state: 'about' } as RouteDataValues },
    { path: 'instructions', loadChildren: '../instructions/instructions.module#InstructionsModule', data: { state: 'instructions' } as RouteDataValues },
    { path: '', component: HomeComponent, data: { state: 'home' } as RouteDataValues },
    { path: 'home', redirectTo: '/' },
    { path: '**', redirectTo: '/' }
  ]
}

我的服务器:

import * as ngUniversal from '@nguniversal/express-engine';
import * as compression from 'compression';
import * as express from 'express';
import * as path from 'path';
import * as appServer from '../dist/server/main.js';
// tslint:disable-next-line:no-require-imports
require('zone.js/dist/zone-node');

const ROOT = path.resolve();

const app = express();

// Server-side rendering
function angularRouter(req, res): void {
  res.render('index', { req, res });
}

// Enable compression
app.use(compression());

// Root route before static files, or it will serve a static index.html, without pre-rendering
app.get('/', angularRouter);

// Serve the static files generated by the CLI (index.html, CSS? JS, assets...)
app.use(express.static('client'));

// Configure Angular Express engine
app.engine('html', ngUniversal.ngExpressEngine({ bootstrap: appServer.AppServerModuleNgFactory }));
app.set('view engine', 'html');
app.set('views', path.join(ROOT, 'client'));

app.listen(3000, () => {
  console.log('Listening on http://localhost:3000');
});

有什么不起作用的主意吗?

1 个答案:

答案 0 :(得分:0)

缺少多个部分,而延迟加载的模块确实存在问题。为了解决这个问题,我已经将服务器与this one进行了比较。

代码已添加到我的服务器:

import 'reflect-metadata';
import 'zone.js/dist/zone-node';

...

import { ngExpressEngine } from '@nguniversal/express-engine';
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';

...

const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('../dist/server/main.js');

...

app.engine('html', ngExpressEngine({
  bootstrap: AppServerModuleNgFactory,
  providers: [
    provideModuleMap(LAZY_MODULE_MAP)
  ]
}));