Angular 9 - The target entry-point has missing dependencies

后端 未结 6 1523
南笙
南笙 2021-02-06 23:09

I have upgraded an Angular library to Angular 9. However when I attempt to use that library in another Angular 9 project I get an error like this:

The target entry-poi

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-06 23:40

    I think I have just worked through the situation you describe. I have an NPM package called my-pkg that contains several "libraries" (created with ng g library lib[1..N], etc). It just so happens that my libN depends on lib1. When I try to use libN in an application, I get the error:

    The target entry-point "my-pkg/libN" has missing dependencies:
     - lib1
    

    Here is how I originally imported lib1 into libN:

    // libN.component.ts
    import { Lib1Comp } from 'lib1';
    

    This works when I build my-pkg. The problem is that 'lib1' doesn't resolve to a top-level package inside my application's node_modules folder. There, it should be known as my-pkg/lib1. So, let's change the import in libN.component.ts:

    // libN.component.ts
    import { Lib1Comp } from 'my-pkg/lib1'; // note lib1 is now prefixed with my-pkg
    

    Of course, we don't have my-pkg installed in my-pkg's node_modules, so it can't find my-pkg/lib1 and now my-pkg doesn't build.

    But what if we were able to "install" my-pkg into it's own node_modules folder? Let's try copying it in there via NPM scripts:

    // package.json
    ...
    "scripts": {
        "copy:lib1": "npx copy .\\dist\\lib1\\**\\* .\\node_modules\\my-pkg\\lib1",
        "build:lib1": "ng build --prod lib1 && npm run copy:lib1",
        // repeat copy/build scripts as needed for libs[2..N]
        "build:pkg": "npm run build:lib1 && npm run build:libN"
    },
    

    Now, we run npm run build:pkg. It builds lib1 then copies it to our local node_modules folder, and now libN can happily import from the path my-pkg/lib1 in both your library and your application!

提交回复
热议问题