Error encountered by Angular's Injector in the AppModule when attempting to access the HttpHandler component

I have been successfully running an app for the past few months with no issues. Now, I am exploring the idea of moving some common services into a library that can be utilized by other applications.

For this project, I decided to avoid using Angular CLI to enhance my learning experience. My current setup involves angular version 7.1.1

The structure of the project is as follows:

- site
    .gitlab-ci.yml
    - core-lib
        package.json
        tsconfig.json
        - src
            index.ts
            core
                my.service.js

    - ui-site
        package.json
        tsconfig.json
        - app
            app.modules.ts
            - table
                table.component.ts
                table.module.ts

In the package.json file of core-lib:

{
  "name": "core-lib",
  "version": "1.0.0",
  "private": true,
  // Dependencies and devDependencies listed here...
}

Within tsconfig.json of core-lib:

{
    "compilerOptions": {
        // Compiler options configured here...
    },
    "include": [
        "./src/**/*"
    ]
}

Inside index.ts under src folder of core-lib:

import { ModuleWithProviders, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
// Additional imports...

@NgModule({
    // Module configuration...
})

export class SharedModule {
  static forRoot(): ModuleWithProviders {
    return { 
        ngModule: SharedModule, 
        providers: [MyService]
    };
  }
}

Currently, MyService is designed to fetch data from a local .json file using httpClient which is demonstrated in this Stackblitz example: https://stackblitz.com/edit/angular-chcpcx

After running tsc.cmd, the compiler generates the necessary .js and .sourcemap files without any errors.

Concerning the package.json file of ui-site:

{
  "name": "ui-site",
  "version": "1.0.0",
  "private": true,
  // Dependencies and devDependencies listed accordingly...
}

Upon executing npm install, the core-lib directory and its files are visible within the node_modules folder of ui-site without any complications.

As for the app.module.ts within ui-site:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
// Other imports...

import { TableModule } from './table/table.module';
import { SharedModule } from '../node_modules/core-lib/src/index';

@NgModule({
  // Module configurations...
})

export class AppModule { }

TableComponent located under app > table in ui-site:

  import { Component } from '@angular/core';
  import { MyService } from '../../../node_modules/core-lib/src/core/my.service'
  // Component code block...

  export class TableComponent {
      // Constructor method and service call...

  }

Error message displayed in the browser:

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

Answer №1

Have you forgotten to include forRoot? In your code snippet, you have

SharedModule.forRoot

@NgModule({
  imports:      [ ..., SharedModule.forRoot, TableModule ],
  //                                       ^
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})

export class AppModule { }

Post-update Instructions

If you are utilizing Windows 10 and a service from an npm linked library, make sure to add

"preserveSymlinks": true

to

angular.json under projects/*/architect/build/options

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

Encountered an `npm ERR! code 1` message following a SyntaxError while executing `npm ci & npm run build` command

I'm having issues building and running a project on an npx dev-server. In my package.json file, I've included "type":"module" within the first {} as advised in this resource. However, when I run npm ci && npm run build ...

Encountering an Issue: "ng new" Command Throws Error Upon Creating Angular Project

I am encountering an issue while using ng new project_name: The error message states "An invalid configuration file was found ['angular.json']. Please delete the file before running the command." I am stuck on this problem and unsure of how to ...

Tips for identifying errors in Angular's HTTPClient.post() method when working with generics

This question has been asked numerous times on SO, but none of the responses address my issue. Therefore, I am rephrasing it. Here is a method (within a service) that performs a post request. makePostReq<T>(reqObj: {url:string, body:any, headerDa ...

Verify the type of email domain (personal or corporate)

Here's an example: isPersonalEmail("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0aea1ada580a7ada1a9aceea3afad">[email protected]</a>") // true isPersonalEmail("<a href="/cdn-cgi/l/email- ...

Images in Angular 2 not appearing until system reboot

When working with angular2 and nodejs to upload an image, I encounter an issue where after node uploads the file to the assets folder, an error occurs when attempting to display it in angular: GET http://localhost:4200/assets/img/3.jpg 404 (Not Found) In ...

Building a dynamic form in Angular with nested JSON data for enhanced reactivity

Although similar questions have been asked before, I am struggling with a large nested JSON value like the following: "Q-1": { "question": { "text": "What are your hopes and goals for this yea ...

What is the best way to ensure type safety in a Promise using Typescript?

It seems that Promises in Typescript can be type-unsafe. This simple example demonstrates that the resolve function accepts undefined, while Promise.then infers the argument to be non-undefined: function f() { return new Promise<number>((resolve) ...

Could I possibly incorporate @mui/material into an Angular project?

Is it possible to use npm install @mui/material in Angular? I am new to Angular and unsure if this will work. If not, are there any alternative Angular libraries that offer Material UI compatibility? ...

What styling options are available in the Angular 6 build configuration for style.js?

Upon inspecting the HTML source code of my single-page application built with Angular 6 using SASS, I stumbled upon this list of files: <script type="text/javascript" src="runtime.js"></script> <script type="text/javascript" src="polyfill ...

I'm puzzled by the error message stating that '<MODULE>' is declared locally but not exported

I am currently working with a TypeScript file that exports a function for sending emails using AWS SES. //ses.tsx let sendEmail = (args: sendmailParamsType) => { let params = { //here I retrieve the parameters from args and proceed to send the e ...

Regex struggles to identify words containing foreign characters

Here is a method I have created to check if a user-input term matches any blacklisted terms: static checkAgainstBlacklist(blacklistTerms, term) { return blacklistTerms.some(word => (new RegExp(`\\b${word}\\b`, 'i&ap ...

What is the best way to retrieve the attribute value of an element using Angular 2?

I am working with an HTML span that contains two hyperlinks. <span><a href="http://appcarvers.cloudaccess.host/index.php?Itemid=207" alt="Shirley Setia">Shirley Setia</a><i class="fa fa-caret-right"></i> <a href="http:// ...

Challenges in Communication between Angular and Spring Boot on an AWS Load Balancer with SSL Enabled

Utilizing an AWS EKS Cluster, I have successfully established communication between Angular pod and Springboot pod without the need for SSL. However, after attempting to add a new certificate from AWS and accessing the Angular application through an ALB, ...

Coverage testing is not embracing all aspects

Currently, I am tackling an Angular 2 project and in the process of writing test cases for the services. It's odd that previously everything was working flawlessly, but now I'm encountering some "no provider" errors such as (No provider for AppSe ...

Troubleshoot Angular2 modules efficiently using source mapping features

Is there a way to debug Angular2 module code using TypeScript source mapping in the browser? After installing my project with the "npm install" command following the steps in https://angular.io/guide/quickstart, I noticed that while source mapping is ava ...

Is there a way to access the badge hidden behind the collapsible menu in bootstrap 4?

After moving from bootstrap 3 to bootstrap 4, my items no longer align properly. I've scoured the entire Internet for a solution, but I've run out of options (and patience.. haha) This is how it currently looks: I want the badge to be positione ...

Is there a way to access the value or key of a JSON property in an Angular template for rendering purposes?

Having trouble displaying the JSON values of certain properties on screen. Utilizing Angular Material table to showcase my JSON response. The code snippet below is responsible for rendering the JSON data: <mat-card-content class="dashboard-card-cont ...

Establish the predefined date for the air-datepicker

I am currently utilizing the air-datepicker inline feature. My objective is to establish the starting date for it. Below is the script detailing my attempt: export function load_datepickers_inline():void { const search_legs_0_datepicker = $("#search_leg ...

Ways to access UserProfile in a different Dialogio

For the implementation of a chatbot, I am utilizing Microsoft's Bot Builder framework. However, upon implementing an alternative path to the dialog flow, I noticed that the user's Profile references are getting lost. Here is the code snippet fr ...

The subscription function in observables may result in values that are undefined

I integrated a new angular 2 library into my application called "angular2-grid". This library is located within the node_modules folder. Furthermore, I created a service as shown below: import { Injectable } from '@angular/core'; import { Htt ...