Extending HttpErrorResponse leads to error Cannot find module '@angular/common/http'

最后都变了- 提交于 2020-06-17 22:54:33

问题


I have a model (defined in separate file) which extends HttpErrorResponse with custom property. Custom property is an interface with few properties:

import { HttpErrorResponse } from '@angular/common/http';

export interface ApiErrorBody {
  id: number;
  code: string;
  message?: string;
  trace?: string;
}

export class ApiErrorResponse extends HttpErrorResponse {
  public error: ApiErrorBody;
}

This code works fine in debug mode, but when compiling to prod, I see error at runtime:

Error: Cannot find module '@angular/common/http'

If I remove extension and just populate the same properties as HttpErrorResponse has, code works fine in prod, but I need to keep extension syntax.

export interface ApiErrorBody {
  id: number;
  code: string;
  message?: string;
  trace?: string;
}

// no extension
export class ApiErrorResponse {
  public error: ApiErrorBody;
  public status: number;
  public message: string;
  piblic url: string;
}

This model is referenced from multiple components and interceptors and all of them are provided with dependency on @angular/common/http, so my questions are - what else I missed here and why it works in debug but doesn't work in prod mode?


回答1:


The problem was in package.json
Package @angular/common was in devDependencies. According to documentation of Electron builder (https://www.electron.build/configuration/contents) dev dependencies never copied to resulting package.

Moving dependencies that start with "@angular" to dependencies from devDependencies section in package.json solved the issues.



来源:https://stackoverflow.com/questions/62189392/extending-httperrorresponse-leads-to-error-cannot-find-module-angular-common-h

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!