找不到模块'./app/server.app.module.ngfactory'
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了找不到模块'./app/server.app.module.ngfactory'相关的知识,希望对你有一定的参考价值。
构建Angular aot时出现以下错误:
找不到模块'./app/server.app.module.ngfactory'
您能帮忙确定什么地方出了问题吗?
main.server.aot.ts包含以下内容:
import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import 'rxjs/Rx';
import * as express from 'express';
import { ngExpressEngine } from '@nguniversal/express-engine';
import { enableProdMode } from '@angular/core';
import { ServerAppModuleNgFactory } from './ngfactory/app/server-app.module.ngfactory';
enableProdMode();
const app = express();
app.engine('html', ngExpressEngine({
bootstrap: ServerAppModuleNgFactory
}));
require('./main.server.shared').confApp(app);
这是tsconfig.server.aot
{
"extends": "./tsconfig.server.json",
"angularCompilerOptions": {
"genDir": "ngfactory",
"entryModule": "./app/server-app.module#ServerAppModule"
},
"include": [
"main.server.aot.ts"
]
}
答案
由于您无法共享代码,建议您遵循此处提到的步骤。 查看您的配置差异。
几点建议:
1. No need of "genDir": "ngfactory" . Remove it from tsconfig.server.json
2. Lazy import : const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main.bundle');
3. Server.ts -
// These are important and needed before anything else
import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import { renderModuleFactory } from '@angular/platform-server';
import { enableProdMode } from '@angular/core';
import * as express from 'express';
import { join } from 'path';
import { readFileSync } from 'fs';
// Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode();
// Express server
const app = express();
const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'dist');
// Our index.html we'll use as our template
const template = readFileSync(join(DIST_FOLDER, 'browser', 'index.html')).toString();
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main.bundle');
const { provideModuleMap } = require('@nguniversal/module-map-ngfactory-loader');
app.engine('html', (_, options, callback) => {
renderModuleFactory(AppServerModuleNgFactory, {
// Our index.html
document: template,
url: options.req.url,
// DI so that we can get lazy-loading to work differently (since we need it to just instantly render it)
extraProviders: [
provideModuleMap(LAZY_MODULE_MAP)
]
}).then(html => {
callback(null, html);
});
});
app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, 'browser'));
// Server static files from /browser
app.get('*.*', express.static(join(DIST_FOLDER, 'browser')));
// All regular routes use the Universal engine
app.get('*', (req, res) => {
res.render(join(DIST_FOLDER, 'browser', 'index.html'), { req });
});
// Start up the Node server
app.listen(PORT, () => {
console.log(`Node server listening on http://localhost:${PORT}`);
以上是关于找不到模块'./app/server.app.module.ngfactory'的主要内容,如果未能解决你的问题,请参考以下文章
text Webpack编译,找不到模块:错误:无法解析模块'步伐'