angulartypescriptnode-modulesangular-librarynpm-package

Publishing an Angular library with an external dependency


I'm writing an Angular library that includes functionality from an external dependency.

For the sake of this example, the library includes a service class that uses axios to fetch data.

When I publish the library and install in a project, I get Error: Module not found: Error: Can't resolve 'axios' in '/node_modules/my-library/fesm2022.

I understand that this error occurs because 'axios' is not actually bundled into the code in my dist folder. Everything needed to ship must be included in the dist folder, but I'm not sure how to resolve that.

I've created an example repo here. I've been using yalc to simulate publishing the package.


Solution

  • It turns out this has nothing to do with Angular. This was occurring on my version published locally with yalc. No issues when published to NPM.

    When you install an NPM package which has say axios as a dependency, your node_modules structure looks like this:

    - node_modules
      - your-package
      - axios
    

    However, yalc does not install dependencies of dependencies. So axios was, indeed, a missing module.

    - .yalc
    ...
    - node_modules
     - my-package