Search code examples
angularangular-cli

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


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?


Solution

  • 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.