New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constructor parameter in service is not compatible with dependency injection after build #2487
Comments
This was a bug that will be fixed in the next Angular FW release. |
Closing as per above. |
Does that mean Angular 15.x? or 16? :) |
The next patch of v15. |
If you're curious: angular/angular#48156 |
After changing the tsconfig to set Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with `EnvironmentInjector#runInContext`. Find more at https://angular.io/errors/NG0203 I'm only leveraging the @Injectable({
providedIn: 'root'
})
export class NgxEnvironmentService<T> {
environment: T;
constructor(
@Inject(ENVIRONMENT_CONFIG)
private readonly environmentConfig: IEnvironmentConfig,
@Inject(PLATFORM_ID)
private readonly platformId: string,
) {
if (isPlatformBrowser(this.platformId)) {
this.environment = this.getEnvironmentValues<T>();
}
}
private getEnvironmentValues = <T>(): T => {
...
};
} Any insight on this? https://angular.io/errors/NG0203 isn't particularly helpful |
Is there a stacktrace you can share? The runtime semantics didn't change when the compiler became more strict. |
Here's the stacktrace: ERROR Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with `EnvironmentInjector#runInContext`. Find more at https://angular.io/errors/NG0203
at injectInjectorOnly (core.mjs:731:15)
at Module.ɵɵinject (core.mjs:742:60)
at NgxEnvironmentService_Factory (ngx-environment.service.ts:11:35)
at Object.EnvironmentService_Factory [as factory] (environment.service.ts:9:32)
at R3Injector.hydrate (core.mjs:8780:35)
at R3Injector.get (core.mjs:8668:33)
at ChainedInjector.get (core.mjs:13929:36)
at lookupTokenUsingModuleInjector (core.mjs:3547:39)
at getOrCreateInjectable (core.mjs:3592:12)
at Module.ɵɵdirectiveInject (core.mjs:10971:12)
handleError @ core.mjs:9229
(anonymous) @ core.mjs:27407
invoke @ zone.js:375
run @ zone.js:134
runOutsideAngular @ core.mjs:26496
(anonymous) @ core.mjs:27407
invoke @ zone.js:375
onInvoke @ core.mjs:26597
invoke @ zone.js:374
run @ zone.js:134
(anonymous) @ zone.js:1278
invokeTask @ zone.js:409
onInvokeTask @ core.mjs:26279
invokeTask @ zone.js:408
onInvokeTask @ core.mjs:26584
invokeTask @ zone.js:408
runTask @ zone.js:178
drainMicroTaskQueue @ zone.js:588
Promise.then (async)
nativeScheduleMicroTask @ zone.js:564
scheduleMicroTask @ zone.js:575
scheduleTask @ zone.js:399
scheduleTask @ zone.js:221
scheduleMicroTask @ zone.js:241
scheduleResolveOrReject @ zone.js:1268
then @ zone.js:1464
bootstrapModule @ core.mjs:27321
4431 @ main.ts:6
__webpack_require__ @ bootstrap:19
__webpack_exec__ @ ngx-environment.ts:3
(anonymous) @ ngx-environment.ts:3
__webpack_require__.O @ chunk loaded:23
(anonymous) @ ngx-environment.ts:3
webpackJsonpCallback @ jsonp chunk loading:34
(anonymous) @ main.js:2 The implementation of this library-provided service is: import { Injectable } from '@angular/core';
import { NgxEnvironmentService } from 'ngx-environment';
import { IEnvironment } from '../interfaces';
@Injectable({
providedIn: 'root'
})
export class EnvironmentService extends NgxEnvironmentService<IEnvironment> {} Nothing too fancy. I'm just extending a service class and providing the type of the value returned by the I've seen some posts like: angular/angular#46419, however my library's package.json only specifies {
"name": "ngx-environment",
"version": "15.0.1",
"peerDependencies": {
"@angular/common": ">=15.0.0",
"@angular/core": ">=15.0.0",
"rxjs": ">=7.5.6",
"tslib": ">=2.4.0"
}
}
|
The stack trace looks normal, but I suspect that you're seeing the effect of having two copies of |
That certainly explains why the source code works as expected when used directly in a project, but why/how would that happen if my package.json for the library only contains peer dependencies? The resulting {
"name": "ngx-environment",
"version": "15.0.1",
"peerDependencies": {
"@angular/common": ">=15.0.0",
"@angular/core": ">=15.0.0",
"rxjs": ">=7.5.6"
},
"module": "fesm2015/ngx-environment.mjs",
"es2020": "fesm2020/ngx-environment.mjs",
"esm2020": "esm2020/ngx-environment.mjs",
"fesm2020": "fesm2020/ngx-environment.mjs",
"fesm2015": "fesm2015/ngx-environment.mjs",
"typings": "index.d.ts",
"exports": {
"./package.json": {
"default": "./package.json"
},
".": {
"types": "./index.d.ts",
"esm2020": "./esm2020/ngx-environment.mjs",
"es2020": "./fesm2020/ngx-environment.mjs",
"es2015": "./fesm2015/ngx-environment.mjs",
"node": "./fesm2015/ngx-environment.mjs",
"default": "./fesm2020/ngx-environment.mjs"
}
},
"sideEffects": false,
"dependencies": {
"tslib": ">=2.4.0"
}
} |
It typically happens when you (sym)link the library into another application, where the link has access to a different |
Hmm. For testing, I have specified the instal location as: "ngx-environment": "/Users/btaylor/work/angular-apps/clinical-authoring-common/ngx-environment/angular-15/dist/ngx-environment", but that doesn't automatically symlink to the best of my knowledge... please correct me if I'm wrong |
I must be wrong, because when I added a path to my "paths": {
"@angular/*": ["node_modules/@angular/*"],
}, and now the runtime error is gone. |
Looks like the local install path was the root cause of all of the issues. Once I published the package to npm and installed from there, everything works as expected. Do you know how I can avoid this in the future? Thank you very much for the replies and helping debug this issue, I greatly appreciate it. |
This way there is no symlink indeed, but every source file in the |
Interesting. I will certainly remember that in the future. Thanks again for all your help. |
This issue has been automatically locked due to inactivity. |
Type of Issue
Description
A bug: please describe the error that you encountered
In Angular 15 / ng-pckagr 15, after building and installing my library into another project, a service provided in my library reports:
The injectable EnvironmentService inherits its constructor from NgxEnvironmentService, but the latter has a constructor parameter that is not compatible with dependency injection. Either add an explicit constructor to EnvironmentService or change NgxEnvironmentService's constructor to use parameters that are valid for DI.(-992016)
However, when using the same service in test code within the library, everything works as expected
A feature: please describe your use case and motivation
I do not want developers leveraging my library to have to provide constructor arguments when extending this service class if they are not doing additional work in the constructor.
How To Reproduce
This is a private, scoped package, but I am more than happy to provide all source code.
To reproduce the issue, I build the library and install it into another Angular application using a local file path in package.json
Expected Behaviour
In all prior versions of this library, from Angular 11 - 14, everything works as expected. Updating from Angular/ng-packagr 14 to 15 builds and tests fine, but fails when implemented in another app.
Version Information
Please include all version numbers that might be relevant, e.g. third-party libraries
The text was updated successfully, but these errors were encountered: