生产构建 ng build --prod 时出现错误

I am getting error while production build ng build --prod

生产构建项目在 angular 4.4.4 上时出现以下错误,错误如下。

警告 5113:8 缺少字体大小。忽略。

ERROR in ./src/main.ts Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in '/home/mnt/.local/share/Trash/files/front_end/src' ERROR in Error: Cannot determine the module for class AddWebsiteAndCampaign in /home/mnt/.local/share/Trash/files/front_end/src/app/campaign/campaign-main/campaign-main.component.ts! Add AddWebsiteAndCampaign to the NgModule to fix it. Cannot determine the module for class CompanyEditComponent in /home/mnt/.local/share/Trash/files/front_end/src/app/company/company-edit/company-edit.component.ts! Add CompanyEditComponent to the NgModule to fix it. at syntaxError (/home/mnt/.local/share/Trash/files/front_end/node_modules/@angular/compiler/bundles/compiler.umd.js:1729:34) at analyzeAndValidateNgModules (/home/mnt/.local/share/Trash/files/front_end/node_modules/@angular/compiler/bundles/compiler.umd.js:24306:15) at AotCompiler.analyzeModulesAsync (/home/mnt/.local/share/Trash/files/front_end/node_modules/@angular/compiler/bundles/compiler.umd.js:23937:46) at CodeGenerator.codegen (/home/mnt/.local/share/Trash/files/front_end/node_modules/@angular/compiler-cli/src/codegen.js:32:14) at Function.NgTools_InternalApi_NG_2.codeGen (/home/mnt/.local/share/Trash/files/front_end/node_modules/@angular/compiler-cli/src/ngtools_api.js:73:30) at _donePromise.Promise.resolve.then (/home/mnt/.local/share/Trash/files/front_end/node_modules/@ngtools/webpack/src/plugin.js:428:58) at process._tickCallback (internal/process/next_tick.js:68:7) at Function.Module.runMain (internal/modules/cjs/loader.js:832:11) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

当我使用 ng build --env=prod 生成构建时,构建不是生产构建,但是当我按照 angular 文档 ng build --prod 使用命令时,上面的错误发生了。 我正在使用 ubuntu 18.

您收到的错误表明您有几个未在任何模块中声明的组件(在错误消息中命名)。

如消息所示,您必须在模块中声明这些组件。

non-prod 构建工作的原因是因为它实际上并没有编译所有内容 - 一些代码被解释,并且完成的检查并不完整。

从这里很难调试,但你可以这样做:

  • 查看相关的@NgModule-s,是否有语法错误
  • 检查相关组件是否添加到合适的@NgModule中(同时检查exports和declarations)
  • 删除整个 node_modules 文件夹,然后通过 npm install
  • 重新安装它们
  • 同时检查 main.ts 和所有引用的文件