npm启动时无法读取未定义的属性"moduleType" [英] Cannot read property 'moduleType' of undefined while npm start

查看:92
本文介绍了npm启动时无法读取未定义的属性"moduleType"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发支持使用角度cli(1.7.3)进行服务器端渲染的应用程序.

I am working on the application which supports server-side rendering with angular cli (1.7.3).

运行命令 npm start build会成功创建,但是在服务器上运行应用程序时会显示错误信息

While running the command npm start build will create successfully but while running application on the server it throws error showing

无法读取未定义的属性'moduleType'

Cannot read property 'moduleType' of undefined

我假设错误来自 server.ts 文件,我在其中导入 AppServerModuleNgFactory 这样的文件:

I assume the error is coming from server.ts file where I am importing AppServerModuleNgFactory like this :

const {  AppServerModuleNgFactory } = (module as any).exports;
// import { AppServerModuleNgFactory } from '../dist/ngfactory/src/app/app.server.module.ngfactory'

PS:这在有角度的 4.4.0 上运行良好,但在 5.2.0

PS: This was working fine with angular 4.4.0 but not with 5.2.0

请让我知道是否需要更多信息.

Please let me know if needed more information.

推荐答案

在数小时的谷歌搜索后解决了该问题.

Solved this probelm after houres of googling.

实际上问题出在ng5中,他们在构建时更改了某些配置,例如移除ngfactory等.因此 server.ts 在服务器端渲染时无法找到 AppServerModuleNgFactory 模块.

Actually the problem is in ng5 they had changed some configuration while build like removal of ngfactory etc. so server.ts is unable to find AppServerModuleNgFactory module while server side rendering.

所以如果找不到这样的话,我必须从新路径更改位置

so I had to change the location from new path if not found like this

const {  AppServerModuleNgFactory } = (module as any).exports;
....
some code 
....

if ( typeof(AppServerModuleNgFactory) == 'undefined') {
  const { AppServerModuleNgFactory } = require('../dist/out-tsc/src/app/app.server.module.ngfactory');
  renderModuleFactory(AppServerModuleNgFactory, opts)
  .then(html => callback(null, html));
} else {
  renderModuleFactory(AppServerModuleNgFactory, opts)
  .then(html => callback(null, html));
}

这篇关于npm启动时无法读取未定义的属性"moduleType"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆