angularangular-cliangular6angular-cli-v6

angular6 how to import a library in another library?


As the title suggests, I've generated a base library using ng generate library lib; and then I generated another library named lib2. I hope to use some base components or service from lib, so I imported lib in the lib2 module, but npm run build lib2 failed.

I recieved this error: error TS2307: Cannot find module 'lib'.

How can I properly import the base library? Any help is appreciated.

I put my test code in https://github.com/dyh333/ng6-test


Solution

  • You will need to build your libs first, see this question Angular 6 CLI -> how to make ng build build project + libraries

    If you take a look in the tsconfig.json file, it has been modified by adding paths for the libs - hence you need to build them to populate the dist folder.

    "paths": {
      "lib": [
        "dist/lib"
      ],
      "lib2": [
        "dist/lib2"
      ]
    }