1

I'm developing a Angular library. I've setup the library like this:

ng new mylibrary --create-application false
ng g library mylib

Now, I can ng build this and create an app that uses it, like this:

ng new myapp
npm install ../mylibrary/dist/mylib and add the MylibModule to the imports of my app.module.ts

This works fine, by default the library has a component that I can use. There also is a dependency injected service in the library.. also works fine.

Now I want to extend my library with http calls, so I add the HttpClientModule to the import of my library module, and the HttpClient to the constructor of my service. I rebuild the lib with ng build and on the app I re-run npm install.. and run with ng serve and now my app throws:

StaticInjectorError(Platform: core)[HttpHandler -> Injector]: NullInjectorError: No provider for Injector!

I guess I somehow have to tell the library to use the injected HttpClient from the app, but how?

EDIT Adding repo: https://github.com/floreseken/NgLibDI

Flores
  • 8,226
  • 5
  • 49
  • 81
  • Have you added HttpClientModule to your list of imports in your main module? – Mickers Jul 29 '19 at 15:20
  • Yes, does not help – Flores Jul 29 '19 at 15:24
  • Does your library contain any injectable services? If so in your app you would need to add these under providers. It's hard to see what the problem could be as there's no code to view. – Mickers Jul 29 '19 at 15:27
  • Tried that. Same error. I just added a repo which shows the behaviour. – Flores Jul 29 '19 at 15:33
  • Btw.. the library has a injectable service which works fine without adding it as provider.. as long as I don't add the HttpClientModule – Flores Jul 29 '19 at 15:34
  • Take a look at this [post](https://stackoverflow.com/questions/40396070/angular2-module-how-can-i-import-a-service-from-another-module). Try exporting MylibService from your library then import it into your app and add it as a provider. – Mickers Jul 29 '19 at 16:21

1 Answers1

2

It is because of the module linking. If you use "ng server --aot", you will not face the issue. Can you remove the links and copy the module directly to node_modules

open the C:\Git\NgLibDI\myapp\node_modules and delete the "mylib" link and manually copy myLib from the Dist to the node_modules of the myapp

Aypn
  • 309
  • 2
  • 7
  • 1
    check. I also found out that setting "preserveSymlinks": true, in the angular.json file fixes it.. that is the nicest solution I guess. – Flores Jul 29 '19 at 17:59