[英]Angular Universal getting the "No NgModule metadata found for '[object Object]'" error
I just updated my Angular app from 5 to 7. In v. 5 I was able to do Server Side Rendering (SSR) just fine.我刚刚将我的 Angular 应用程序从 5 更新到 7。在 v. 5 中,我能够很好地进行服务器端渲染 (SSR)。
After the update, SSR fails with the following error (the build process works fine):更新后,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)
Given this SO question , I am not using "bundleDependencies": "all"
, I've even tried to state explicitly none
in angular.json
, but it did not solve the issue.鉴于这个SO question ,我没有使用
"bundleDependencies": "all"
,我什至试图在angular.json
明确声明none
,但它没有解决问题。
I'm using this configuration in angular.json
:我在
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"
}
}
}
I'm also lazy loading almost all modules, if that matters (it did not in v. 5):我也懒加载几乎所有模块,如果这很重要(它没有在第 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: '/' }
]
}
My server:我的服务器:
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');
});
Any idea of what's not working?知道什么不起作用吗?
There were multiple pieces missing, and it was indeed a problem with lazy loaded modules.有多个部分丢失,确实是延迟加载模块的问题。 To solve I've compared my server to this one .
为了解决这个问题,我将我的服务器与这个服务器进行了比较。
Code added to my server:添加到我的服务器的代码:
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)
]
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.