多个Angular库在生产模式下不起作用

时间:2020-05-27 07:32:26

标签: angular typescript npm

我创建了一个由两个Angular库组成的Angular项目。您可以找到源代码here。之后,我建立了一个库并通过npm共享了生成的dist文件夹。这是软件包:https://www.npmjs.com/package/@agilie/angular-helpies

在那之后,我试图使用该库中的拦截器并像这样导入

import {DuplicateRequestFilter} from '@agilie/angular-helpies/interceptors';

一切都可以在开发人员模式下正常工作,但是当我尝试在生产模式下构建项目时会出现错误

ERROR in ./src/app/app.module.ngfactory.js
Module not found: Error: Can't resolve 'interceptors' in '/Users/sergey/libs/test-helpies/src/app'
resolve 'interceptors' in '/Users/sergey/libs/test-helpies/src/app'
  Parsed request is a module
  using description file: /Users/sergey/libs/test-helpies/package.json (relative path: ./src/app)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      looking for modules in /Users/sergey/libs/test-helpies/
        using description file: /Users/sergey/libs/test-helpies/package.json (relative path: .)
          Field 'browser' doesn't contain a valid alias configuration
          using description file: /Users/sergey/libs/test-helpies/package.json (relative path: ./interceptors)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/interceptors doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/interceptors.ts doesn't exist
            .tsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/interceptors.tsx doesn't exist
            .mjs
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/interceptors.mjs doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/interceptors.js doesn't exist
            as directory
              /Users/sergey/libs/test-helpies/interceptors doesn't exist
      /Users/sergey/libs/test-helpies/src/app/node_modules doesn't exist or is not a directory
      /Users/sergey/libs/test-helpies/src/node_modules doesn't exist or is not a directory
      /Users/sergey/libs/node_modules doesn't exist or is not a directory
      /Users/sergey/node_modules doesn't exist or is not a directory
      /Users/node_modules doesn't exist or is not a directory
      /node_modules doesn't exist or is not a directory
      looking for modules in /Users/sergey/libs/test-helpies/node_modules
        using description file: /Users/sergey/libs/test-helpies/package.json (relative path: ./node_modules)
          Field 'browser' doesn't contain a valid alias configuration
          using description file: /Users/sergey/libs/test-helpies/package.json (relative path: ./node_modules/interceptors)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/node_modules/interceptors doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/node_modules/interceptors.ts doesn't exist
            .tsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/node_modules/interceptors.tsx doesn't exist
            .mjs
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/node_modules/interceptors.mjs doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/node_modules/interceptors.js doesn't exist
            as directory
              /Users/sergey/libs/test-helpies/node_modules/interceptors doesn't exist
[/Users/sergey/libs/test-helpies/interceptors]
[/Users/sergey/libs/test-helpies/interceptors.ts]
[/Users/sergey/libs/test-helpies/interceptors.tsx]
[/Users/sergey/libs/test-helpies/interceptors.mjs]
[/Users/sergey/libs/test-helpies/interceptors.js]
[/Users/sergey/libs/test-helpies/src/app/node_modules]
[/Users/sergey/libs/test-helpies/src/node_modules]
[/Users/sergey/libs/node_modules]
[/Users/sergey/node_modules]
[/Users/node_modules]
[/node_modules]
[/Users/sergey/libs/test-helpies/node_modules/interceptors]
[/Users/sergey/libs/test-helpies/node_modules/interceptors.ts]
[/Users/sergey/libs/test-helpies/node_modules/interceptors.tsx]
[/Users/sergey/libs/test-helpies/node_modules/interceptors.mjs]
[/Users/sergey/libs/test-helpies/node_modules/interceptors.js]
 @ ./src/app/app.module.ngfactory.js 14:0-35 15:1945-1970
 @ ./src/main.ts
 @ multi ./src/main.ts

您可以使用以下演示项目来重现错误:https://github.com/SergeyMell/test-helpies

我在做什么错?任何帮助表示赞赏。

更新

Field 'browser' doesn't contain a valid alias configuration中的答案似乎提供了解决方案,以解决在使用中的应用程序中实际损坏的程序包。就我所知,我在构建或分发程序包时遇到一些问题,因此应予以解决。

2 个答案:

答案 0 :(得分:1)

该错误是由于您导入中的/ interceptors造成的。

您在拦截器文件夹中也没有public-api.ts

在这里有一个参考,您的public-api.ts应该在哪里,以及如何正确导出您的类。像@ angular / common / http一样已导出。

https://github.com/angular/angular/tree/master/packages/common/http

答案 1 :(得分:0)

最后,我设法克服了这个问题。问题出在软件包命名中。只要我的根软件包的名称为@agilie/angular-helpies,子软件包就应分别命名为@agilie/angular-helpies/interceptors@agilie/angular-helpies/decorators,而不是简单的interceptorsdecorators

这看起来很奇怪,因为如果我尝试使用该名称生成lib,则会收到错误Project name "@agilie/angular-helpies/<some name here>" is invalid。另外,当我手动重命名软件包时,也会收到警告String violates the pattern。但是,它起作用。

可能可以通过其他方式(npm链接或其他不同)解决此问题,但是我找到了一种解决方案。实际上,如果您查看https://github.com/angular/angular/blob/master/packages/common/http/package.json,例如Angular就有类似的命名