Angular CLI - 'Cannot find module' when using custom library created with 'ng new library'

最后都变了- 提交于 2020-12-08 07:59:19

问题


I'm trying to consume my own library after creating it with the Angular CLI using ng new library lib-name.

I'm trying to import the library as documented, this way:

import {MyLibModule} from 'ngx-mylib';

But I get the following error:

 error TS2307: Cannot find module 'ngx-mylib'.

I did ng build ngx-mylib and also verified my tsconfig.json has the following paths (which are added automatically using the CLI):

"paths": {
      "ngx-mylib": [
        "dist/ngx-chartjs"
      ],
      "ngx-mylib/*": [
        "dist/ngx-chartjs/*"
      ]
    }

public-api.ts:

export * from './lib/charts.service';
export * from './lib/chart.component';
export * from './lib/charts.module';

What I'm missing?


回答1:


After build your library you have to pack it.

Go to your dist folder and run

npm pack

this will create a your-lib-name.tgz file and then you can use it on your projects.

Use it like this:

npm install path-to-your-tgz-file/your-lib.tgz

You can also publish it to npm or to a private repository.




回答2:


try to build it again and then run your project sometimes angular can't build the library properly



来源:https://stackoverflow.com/questions/58110976/angular-cli-cannot-find-module-when-using-custom-library-created-with-ng-ne

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