Angular 通用出现 "No NgModule metadata found for '[object Object]'" 错误
Angular Universal getting the "No NgModule metadata found for '[object Object]'" error
我刚刚将我的 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)
鉴于此 ,我没有使用 "bundleDependencies": "all"
,我什至尝试在 angular.json
中明确说明 none
,但它没有解决问题.
我在 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');
});
知道什么不起作用吗?
少了好几块,确实是懒加载模块的问题。为了解决这个问题,我将我的服务器与 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)
]
}));
我刚刚将我的 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)
鉴于此 "bundleDependencies": "all"
,我什至尝试在 angular.json
中明确说明 none
,但它没有解决问题.
我在 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');
});
知道什么不起作用吗?
少了好几块,确实是懒加载模块的问题。为了解决这个问题,我将我的服务器与 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)
]
}));