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

The autocomplete feature is not displaying the suggested text at the top when choosing data

Recently started using react native and I'm currently working on implementing an AutoCompleteTextView. I have a json file that contains country names which need to be displayed in the autocomplete view. Although I am able to display the names of all c ...

What is the preferred method for validating an Angular form - ng-model or form input name?

When it comes to validating an input field and providing feedback, there are two methods that I have noticed: <form name="myform" ng-submit="myform && myFunc()"> <input name="foo" ng-model="foo" ...

Discover the most effective method for identifying duplicate items within an array

I'm currently working with angular4 and facing a challenge of displaying a list containing only unique values. Whenever I access an API, it returns an array from which I have to filter out repeated data. The API will be accessed periodically, and the ...

Leveraging both Angular Material UI and Tailwind CSS simultaneously

Recently, I've been incorporating Material UI with Angular. With the deprecation of flexlayout and the rise of Tailwind as a recommended alternative, I'm wondering if it's feasible to utilize both Material UI and Tailwind in tandem. How can ...

ElevationScroll expects the 'children' prop to be a single child component of type 'ReactElement<any, string>'

I am currently working on integrating the Elevate AppBar from Material UI into my application using the following code: interface Props { children: React.ReactElement; } export const ElevationScroll = ({children}: Props) => { const trigger = u ...

What could be causing the conditional div to malfunction in Angular?

There are three conditional div elements on a page, each meant to be displayed based on specific conditions. <div *ngIf="isAvailable=='true'"> <form> <div class="form-group"> <label for ...

When troubleshooting Apache CouchDB + npm-registry-couchapp setup, facing issues with npm installation is common

I am currently in the process of setting up a private npm registry. After configuring couchdb and verifying that it is replicating successfully by executing the curl request /_all_docs, I encountered an issue with running npm install. The problem seems to ...

Unable to access or modify NGXS state within an Angular 8 NativeScript application

After gaining experience with Angular, I decided to experiment with the NGXS data store while following a NativeScript tutorial. Despite trying several NGXS tutorials, I couldn't get the state to update without any errors from Android Studio or NS. N ...

Error: The command you are looking for does not exist in

Every time I attempt to execute npm create-react-app, I encounter an error stating it is an unknown command. How can I resolve this issue? Microsoft Windows [Version 10.0.22631.3527] (c) Microsoft Corporation. All rights reserved. G:\Project\f ...

The type 'HTMLDivElement | null' cannot be assigned to the type 'HTMLDivElement' in this context

Struggling with a scroll function to maintain position while scrolling up or down - encountering an error: Error: Type 'HTMLDivElement | null' is not assignable to type 'HTMLDivElement'. Type 'null' is not assignable to type & ...

Issues with slider functionality in a Next.js application styled with Tailwind CSS

"use client"; import React, { useState } from "react"; const textData = [ { id: 1, text: "Text 1 Description", }, { id: 2, text: "Text 2 Description", }, { id: 3, text: "Text 3 ...

Execute a nested package.json script

I have a folder named package-scripts where I keep all the scripts for building in the react environment: const npsUtils = require('nps-utils') module.exports = { scripts: { default: 'react-scripts start', build: 'react- ...

Compiler unable to determine Generic type if not explicitly specified

Here is a simple code snippet that I am working with: class Model { prop1: number; } class A<TModel> { constructor(p: (model: TModel) => any) {} bar = (): A<TModel> => { return this; } } function foo<T>(p: ...

Obtain the selected type from a tuple after filtering

I have a tuple with multiple objects stored in it. const repos = [ { name: 'react', type: 'JS' }, { name: 'angular', type: 'TS' }, ] as const const RepoTypes = typeof repos const jsRepoTypes = FilterRepos<&a ...

Prevent form validation in ReactiveForm when a hidden field is present

I am encountering an issue with the validation of a button in my form. The button is disabled until the form is valid, which works perfectly when all fields are visible. However, if a field is hidden due to a condition (ngIf), the validation still occurs. ...

The error message "InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe' in Angular 6 and Firebase" indicates a problem with the data being passed to the AsyncPipe in

**Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'. ** I am encountering a problem with unsubscribing from the observable. My Angular-cli version is 6.0.3 This is the HTML code in home.component.html <div class ...

Utilize modules and generate objects in various locations

const express = require('express'); const app = express(); This scenario focuses on the usage of the express module, but it can be applied to any module where you need to require the module and use its constructor. If we look at the general co ...

As a result of a recent breach in security protocols, all user tokens have been rendered invalid

After uninstalling the newer version of node that was on my machine (v8.11.3), I installed an earlier version, v6.11.0, because my project requires this version for a grunt build. However, upon trying to restore the packages for my project using npm instal ...

Different return types of a function in Typescript when the parameter is either undefined or its type is explicitly asserted vary

Addressing Complexity This code may seem simple, but the use case it serves is quite complex: type CustomFunction = <B extends ['A']>( param?: B ) => B extends undefined ? 'TYPE_1' : 'TYPE_2' // Usage examples: co ...

Module for Npm that includes unique code for both proxy support and non-proxy support configurations

Is there a way to develop a javascript library (available as a module on npm) with multiple implementations based on the level of proxy support in the environment where it is executed (transpiled to)? From my understanding, babel may not easily transpile ...