Encountered an issue during the migration process from AngularJS to Angular: This particular constructor is not compatible with Angular's Dependency

For days, I've been struggling to figure out why my browser console is showing this error. Here's the full stack trace:

Unhandled Promise rejection: NG0202: This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.

Please ensure that 1) the type for the parameter at index 0 is correct and 2) the correct Angular decorators are defined for this class and its ancestors. ; Zone: <root> ; Task: Promise.then ; Value: Error: NG0202: This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.

Please ensure that 1) the type for the parameter at index 0 is correct and 2) the correct Angular decorators are defined for this class and its ancestors.
    at ɵɵinvalidFactoryDep (injector_compatibility.ts:94:9)
    at Object.AppModule_Factory [as useFactory] (ɵfac.js? [sm]:1:1)
    at Object.factory (r3_injector.ts:450:32)
    at R3Injector.hydrate (r3_injector.ts:352:29)
    at R3Injector.get (r3_injector.ts:235:23)
    at injectInjectorOnly (injector_compatibility.ts:64:29)
    at ɵɵinject (injector_compatibility.ts:81:58)
    at useValue (provider_collection.ts:249:62)
    at R3Injector.resolveInjectorInitializers (r3_injector.ts:284:9)
    at new NgModuleRef (ng_module_ref.ts:86:22) Error: NG0202: This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.

Please ensure that 1) the type for the parameter at index 0 is correct and 2) the correct Angular decorators are defined for this class and its ancestors.
    at ɵɵinvalidFactoryDep (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:4774:11)
    at Object.AppModule_Factory [as useFactory] (ng:///AppModule/ɵfac.js:4:37)
    at Object.factory (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:6968:38)
    at R3Injector.hydrate (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:6881:35)
    at R3Injector.get (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:6769:33)
    at injectInjectorOnly (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:4758:33)
    at ɵɵinject (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:4762:61)
    at useValue (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:6561:65)
    at R3Injector.resolveInjectorInitializers (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:6818:17)
    at new NgModuleRef (http://localhost:3300/node_modules/@angular/core/fesm2015/core.mjs:21725:26)

This is what my app.module.ts file looks like:

    import "@angular/compiler";
    import "zone.js";
    import {DoBootstrap, NgModule} from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { UpgradeModule } from '@angular/upgrade/static';
    import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
    // import moment from "moment";
    
    @NgModule({
        imports: [
            BrowserModule,
            UpgradeModule
        ],
    })
    export class AppModule implements DoBootstrap {
        constructor(private upgrade: UpgradeModule) { }
        ngDoBootstrap() {
            this.upgrade.bootstrap(document.body, ['CoreApplication'], { strictDi: true });
        }
    }

platformBrowserDynamic().bootstrapModule(AppModule);

I'm utilizing importmap to manage ES modules, and it seems to be working fine. However, upon bootstrapping the application, I encounter the mentioned error. Any assistance would be greatly appreciated.

[EDIT 1]

While attempting to debug the library, I noticed that the error occurs at provider.useFactory(), where the provider object appears as follows:

{
  deps: []
  provide: ƒ AppModule(upgrade)
  useFactory: ƒ AppModule_Factory(t)
  [[Prototype]]: Object
}

The line at https://github.com/angular/angular/blob/211c35358a322f6857c919a2cc80218fbd235649/packages/core/src/di/r3_injector.ts#L450 uses injectArgs to retrieve an array, but in my case, it returns an empty array causing the function to halt. Should I declare a factory somewhere? If so, where should it be declared?

[EDIT 2]

It seems that the function provider.useFactory is defined in this manner:

'function AppModule_Factory(t) {\n  return new (t || jit_AppModule_0)(jit___invalidFactoryDep_1(0));\n}'

Answer №1

Encountered a similar problem and managed to fix it by including an Injection Token in the constructor:

constructor(@Inject(UpgradeModule) private upgrade: UpgradeModule) {}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Uploading multiple files and parsing them in AngularJS

I have successfully created an interface for uploading multiple CSV files. These CSV files are loaded to the client's browser using a custom fileReader service that utilizes $q, then parsed using ngPapaParser and displayed in the view with ngTable. ...

The type undefined cannot be assigned to the type even with a null check

When looking at my code, I encounter an error stating Argument of type 'Definition | undefined' is not assignable to parameter of type 'Definition'. Even though I am checking if the object value is not undefined with if (defs[type] != u ...

Exploring the depths of Angular8: Utilizing formControlName with complex nested

After dedicating numerous hours to tackle this issue, I finally came up with a solution for my formGroup setup: this.frameworkForm = this.formBuilder.group({ id: [null], name: ['', Validators.required], active: [true], pa ...

Outputting data stored in Firestore object

Is there a way to display the content of a Firestore object in Angular Firebase? I am working on a project where I need to retrieve and print the name of a document stored in Firestore. In my Firestore database, I have a parent collection called "nforms" w ...

I'm trying to figure out the best way to successfully pass a prop to another component in TypeScript without running into the frustrating issue of not being able to

I have been facing an issue while trying to pass a prop from a custom object I have defined. The structure of the object is as follows: export type CustomObjectType = { data?: DataObject }; export type DataObject = { id: number; name: stri ...

Top method for transferring server (C# / Razor) data to an AngularJS application

In our use of DNN, we often encounter the need to pass specific context values (such as page id or module-on-page-id) into an AngularJS application. While we have established our own conventions for achieving this, we are interested in hearing about how ot ...

Challenges with Loading JSON Dynamically in Next.js using an NPM Package

In my TypeScript project, I have implemented a functionality where a json configuration file is dynamically loaded based on an enum value passed as a parameter to the getInstance function in my PlatformConfigurationFactory file. public static async getIn ...

Module TypeScript could not be located

Currently, I am in the process of converting my nodejs project from JavaScript to TypeScript. I have updated the file extensions from .js to .ts, but now I am encountering errors with require(). In an attempt to fix this issue, I have added the following c ...

The installation of Angular CLI through npm has unfortunately encountered an error

After following the steps from this post to remove the old installation, I encountered an issue during the last step: [sudo] npm uninstall -g @angular/cli [sudo] npm cache verify [sudo] npm install -g @angular/cli During the final step, I faced difficult ...

Utilizing AngularJs to input new data entries into a form

I have been exploring AngularJS lately as a beginner and am currently working on creating a form that includes a 'create' button to generate a new server record using a service. Here is the Angular code I have written: $scope.createNewRecord = f ...

How can I retrieve the document id from Firestore using Angular?

I attempted to generate an auto document ID in Firestore and retrieve the document ID in Angular 8 using the code provided. However, I am encountering an issue where I only receive the document ID after the execution has been completed. Can someone pleas ...

I am attempting to store the primary array in local storage, but unfortunately, the value is not being saved within the React context API

I attempted to store the main array in local storage and retrieve it as global state, but I am facing an issue where the data is not being saved in the local storage. This file represents my context. import { createContext, useReducer, ReactNode, FC, use ...

esBuild failing to generate typescript declaration files while running in watch mode

Recently dove into using edBuild and I have to say, it's been a breeze to get up and running - simple, fast, and easy. When I execute my esBuild build command WITHOUT WATCH, I can see that the type files (.d.ts) are successfully generated. However, ...

Issue with updating overall expenditure functionality

I'm currently in the process of developing a straightforward shopping cart with the help of simpleCart.js. Up until now, I've managed to successfully showcase items, add them to the basket, and proceed to checkout. However, my next challenge inv ...

Troubleshooting AngularJS Route Issues on Windows Phone 8 with Worklight Integration

I am currently in the process of developing a Hybrid App for iOS, Android, Blackberry, and Windows using IBM Worklight. For the framework, I have chosen to use AngularJS along with HTML, CSS, and Bootstrap. Since this app is a Single Page Application (SP ...

What is the proper error type to use in the useRouteError() function in react-router-dom?

In my React project, I am utilizing the useRouteError() hook provided by react-router-dom to handle any errors that may arise during routing. However, I'm uncertain about the correct type for the error object returned by this hook. Currently, I have ...

Using a static enum in a different class in TypeScript: A guide

After referencing this question and answer on Stack Overflow about setting a static enum inside a TypeScript class, I decided to create my own enum and implement it as a static property in my class. Here is how I did it: /* Input.ts */ enum INPUT_TYPE { T ...

Wrapping text around an image using two distinct Angular components

Can text wrap around an image in Angular even if they are in separate components? Or do the text and image have to be within the same component for this to work, regardless of whether the image is on the left or right side? https://i.stack.imgur.com/hdlxD ...

What sets apart AngularJs's complete $http post from its shorthand counterpart $http.post?

When sending Object data from app.js to homecontroller using two different methods (full $http.post and shorthand $http.post) as shown below: var book = { "title" : $scope.addTitle, "publisher" : $scope.publisherSelected[0], "authors" : $scope ...

Nested ControlGroup in Angular2's ControlArray

I've hit a roadblock trying to iterate through a ControlArray that has Controlgroups in a template. In TypeScript, I successfully created the ControlArray and added some ControlGroups by looping over data fetched from an API. The console displays the ...