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

6
  • Have you added HttpClientModule to your list of imports in your main module? Commented Jul 29, 2019 at 15:20
  • Yes, does not help Commented Jul 29, 2019 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. Commented Jul 29, 2019 at 15:27
  • Tried that. Same error. I just added a repo which shows the behaviour. Commented Jul 29, 2019 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 Commented Jul 29, 2019 at 15:34

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

check. I also found out that setting "preserveSymlinks": true, in the angular.json file fixes it.. that is the nicest solution I guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.