question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ERROR NullInjectorError: R3InjectorError when building a lib and using services who are not providedIn root

See original GitHub issue

🐞 bug report

Affected Package

@angular/core

Is this a regression?

Yes, only happens with ivy. If I disable ivy, everything works as expected

"angularCompilerOptions": {
    "enableIvy": false
}

Description

We are using ionic-native for our crossplatform apps. So we created some libraries to share code between different apps. If we use ionic-native (which has no providedIn root) we add the providers inside forRoot of a module in the library (1), build the library and add this module from our library in our AppModule (2). If we now using the service from the lib (3), the error occurs on start-up

We created an minimal repo where you can see the issue.

(1) https://github.com/paulstelzer/angular-ionic-issue/blob/master/projects/inno-core/src/lib/inno-core.module.ts

(2) https://github.com/paulstelzer/angular-ionic-issue/blob/master/src/app/app.module.ts

(3) https://github.com/paulstelzer/angular-ionic-issue/blob/master/src/app/app.component.ts

🔬 Minimal Reproduction

Github: https://github.com/paulstelzer/angular-ionic-issue

Nice to know: The “dist” folder is already comitted, so you do not need to build the lib. If you want just run npm run lib:build,

  1. npm i
  2. ng serve

The app will show the error like mentioned below

🔥 Exception or Error


core.js:6185 ERROR NullInjectorError: R3InjectorError(AppModule)[PlatformService -> Device -> Device -> Device]: 
  NullInjectorError: No provider for Device!
    at NullInjector.get (http://localhost:4200/vendor.js:36155:27)
    at R3Injector.get (http://localhost:4200/vendor.js:49871:33)
    at R3Injector.get (http://localhost:4200/vendor.js:49871:33)
    at R3Injector.get (http://localhost:4200/vendor.js:49871:33)
    at injectInjectorOnly (http://localhost:4200/vendor.js:36010:33)
    at ɵɵinject (http://localhost:4200/vendor.js:36020:57)
    at Object.PlatformService_Factory [as factory] (http://localhost:4200/main.js:103:356)
    at R3Injector.hydrate (http://localhost:4200/vendor.js:50107:63)
    at R3Injector.get (http://localhost:4200/vendor.js:49859:33)
    at NgModuleRef$1.get (http://localhost:4200/vendor.js:67203:33)

🌍 Your Environment

Angular Version:


Angular CLI: 9.0.7
Node: 10.16.0
OS: win32 x64

Angular: 9.0.7
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.900.7
@angular-devkit/build-angular      0.900.7
@angular-devkit/build-ng-packagr   0.900.7
@angular-devkit/build-optimizer    0.900.7
@angular-devkit/build-webpack      0.900.7
@angular-devkit/core               9.0.7
@angular-devkit/schematics         9.0.7
@ngtools/webpack                   9.0.7
@schematics/angular                9.0.7
@schematics/update                 0.900.7
ng-packagr                         9.0.3
rxjs                               6.5.4
typescript                         3.7.5
webpack                            4.41.2

Anything else relevant? We building the library with ng-packagr! This can also be a bug of ionic-native!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
JoostKcommented, Mar 23, 2020

@mlc-mlapis I wasn’t aware of that issue but it looks to be the same as #35759 for which I commented on how to make it work: https://github.com/angular/angular/issues/35759#issuecomment-601444856.

0reactions
angular-automatic-lock-bot[bot]commented, Jul 9, 2020

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular - ERROR NullInjectorError: R3InjectorError(AppModule)
You can see from the error message that the dependency container is missing a provider for AngularFireDatabase: " No provider for ...
Read more >
NG0201: No provider for {token} found! - Angular
To fix the error ensure that your service is registered in the list of providers of an NgModule or has the @Injectable decorator...
Read more >
[Debugging] NullInjectorError: No provider for {Class}!
In this video, you'll learn what the error " NullInjectorError : No provider for {Class}!" means, how to debug it, and prevent it...
Read more >
OktaAuthModule Not Resolving Correctly When Using Factory ...
console.log(config.issuer);. And it works just fine. Looks like that's how you all are detecting the error. What am I missing here?
Read more >
What could go wrong? How to handle errors in Angular
In the front-end code there was a service, which had an error handler ... This way we could create our own library and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found