Angular class is not an Angular module

前端 未结 2 1035
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-18 07:28

When I build my Angular library, publish it to npm, and use it as a dependency in another project, whenever I try to import on of my module classes into my app.module.

2条回答
  •  心在旅途
    2021-01-18 07:49

    So I figured out what was causing my specific issue. It was two things. Point two is the direct reason for my issue, point one was making much more confusing to debug.

    1. In order to test my built package, I would run ng build, and then cd into the built project in the dist directory, and run npm pack. I would then install my built package as a file dependency in an entirely separate project to ensure I did everything correctly. What I found out was (or I'm assuming) that there was a caching mechanism going on even when doing local file dependency installations. This caching seemed to be tied to the file name of the tgz file that is generated from npm pack. This caching was causing the very first version to be constantly reinstalled not matter how many changes I made to the built library. The fix, for me, was to simply rename the tgz file to something different each time. You should also change the library version number, and that should also work.
    2. My file structure looked like this:
    src
    - public-api.ts
    - lib
    -- button
    --- index.ts
    --- public-api.ts
    --- button.component.ts
    --- button.module.ts
    -- another-module
    -- yet-another-module
    

    The public-api.ts file directly in src looked like this:

    export * from './lib/button'
    // Other exports
    

    The public-api.ts, and the index.ts file under src\lib\button looked like this:

    // public-api.ts
    export * from './button.component.ts'
    export * from './button.module.ts'
    
    // index.ts
    export * from './public-api.ts'
    

    I had assumed that by adding the index.ts files to each directory, the export line in the root public-api.ts would find the index file, and export the files accordingly, but somewhere along the line, this implicit resolution does not work, and, instead, an explicit export is needed. The fix was to change export * from './lib/button' to export * from './lib/button/index.ts' or export * from './lib/button/public-api.ts'.

    Point two was the direct reason for my issue, point one just made it really confusing when trying to debug.

提交回复
热议问题