Fixing error TS2397: Cannot find module in angular custom library

I spent quite a bit of time trying to fix a problem in a project I’m working on where I’m extracting reusable components from an existing application to a new angular library. I’ve followed the usual steps indicated in the guides (like this one https://angular.io/guide/creating-libraries) and found myself with the main app on /src/app while the library is at /project/LIB_NAME/src/...

The angular CLI helpfully added the paths config to the tsconfig.json on the root of my project, like so

{
  "compilerOptions": {
    ...
    "paths": {
      "name-lib": [
        "dist/name-lib"
      ],
      "name-lib/*": [
        "dist/name-lib/*"
      ]
    }
  }
}

This made the IDE and tsc happy to compile my app, but running ng build gave my the annoying error

error TS2307: Cannot find module ‘name-lib’.

I solved it adding the paths also to the tsconfig.app.json file present at in the /src directory

{
  "compilerOptions": {
    ...
    "paths": {
      "ika-admin-lib": [
        "../dist/name-lib"
      ],
      "ika-admin-lib/*": [
        "../dist/name-lib/*"
      ]
    }
}

pay attention to the relative path

Hope it helps

Author: Maurizio Pozzobon

Maurizio has 5+ years developing solutions in the insurance industry. He is passionate about doing the right thing right, so he works in a tight loop with his clients to deliver the best solution possible.