Encountered Angular SSR Serve Error: NullInjectorError - StaticInjectorError in AppServerModule with the following reference:

While working on building an application with Angular's SSR and serving it, I encountered a specific error. All services and components have been properly injected.

Error:

ERROR Error [NullInjectorError]: StaticInjectorError(AppServerModule)[REQUEST]: 
  StaticInjectorError(Platform: core)[REQUEST]:
    NullInjectorError: No provider for REQUEST!
    at NullInjector.get (C:\Users\---\-----\dist\server\main.js:18902:29)
    at resolveToken (C:\Users\---\-----\dist\server\main.js:30060:28)
    at tryResolveToken (C:\Users\---\-----\dist\server\main.js:30004:20)
    at StaticInjector.get (C:\Users\---\-----\dist\server\main.js:29906:24)
    at resolveToken (C:\Users\---\-----\dist\server\main.js:30060:28)
    at tryResolveToken (C:\Users\---\-----\dist\server\main.js:30004:20)
    at StaticInjector.get (C:\Users\---\-----\dist\server\main.js:29906:24)
    at resolveNgModuleDep (C:\Users\---\-----\dist\server\main.js:38371:33)
    at _createClass (C:\Users\---\-----\dist\server\main.js:38420:72)
    at _createProviderInstance (C:\Users\---\-----\dist\server\main.js:38388:30) {
  ngTempTokenPath: null,
  ngTokenPath: [ 'REQUEST' ]
}

My app.server.module.ts:

import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { ServerCookiesModule } from 'ngx-universal-cookies/server';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';
import { ModuleMapLoaderModule } from '@nguniversal/module-map-ngfactory-loader';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { UniversalInterceptor } from './universal.interceptor';
import { AuthenticationServerModule } from './authentication/authentication.server.module';

@NgModule({
  imports: [
    AppModule,
    ServerModule,
    ModuleMapLoaderModule,
    ServerCookiesModule.forRoot(),
    AuthenticationServerModule
  ],
  bootstrap: [AppComponent],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: UniversalInterceptor,
      multi: true,
    },
  ],
})
export class AppServerModule {}

Answer №1

Here's how I successfully resolved the issue.

I inserted the following code snippet into the "server.ts" file. That's it.

server.ts :

app.get('*', (req, res) => {
  res.render(indexHtml, {
    req,
    res,
    providers: [
      {
        provide: 'REQUEST',
        useValue: req,
      },
      {
        provide: 'RESPONSE',
        useValue: res,
      },
    ],
  });
});

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

Application built with Electron and Typescript encounters module-related crash

While working on developing a client using Electron with Typescript, I encountered the following error: The configuration in tsconfig.json looks like this: { "compilerOptions": { "target": "es5", "lib": [ ...

Guide on showing the content of an uploaded file as an object in JavaScript using file reader

When using the file upload function to upload a json file and read its contents, I am encountering an issue where the result is in string format instead of object. How can I display it as an object? Here is my code: .html <div class="form-group"> ...

Tips for dismissing loader in Ionic 4

I'm struggling to programmatically dismiss the loader. I've developed two separate methods - one for displaying the loader and another for dismissing it. These methods are called accordingly when needed. async showLoader() { this.loader = a ...

Importing libraries in TypeScript and JavaScript are not done in the same manner

As I create my own library, I aim for it to be compatible with both javascript and typescript. tsconfig.json { "compilerOptions": { "target": "es2017", "module": "commonjs", &qu ...

Leveraging Angular 8 for Multi-Tenancy Implementation

Currently, I am involved in a project that caters to multiple clients. Utilizing angular themes allows me to customize the background color, font, and other elements for each client. Additionally, I need to repurpose components by adjusting the text in var ...

Issue encountered while utilizing combineReducers: "Error: The assetsReducer returned an undefined value during initialization."

Issue: The "assetsReducer" has returned an undefined value during initialization. When the state passed to the reducer is undefined, it must explicitly return the initial state, which cannot be undefined. If no value is set for this reducer, consider using ...

VS Code using Vue is displaying an error message stating: The property '' does not exist on type '{}'.ts(2339)

While working in Visual Studio Code, I came across the following code snippet: <script lang="ts" setup> const parseCSV = () => { // Code omitted for brevity } } </script> <template> <button @click="parseCSV ...

Cannot utilize the subscribed output value within the filter function

I am in need of assistance with my Angular 7 project. I have successfully implemented a service to call a Json file and output an object array. However, I am facing an issue when trying to filter the objects in the array based on a specific property called ...

Reactive form enabled for Angular control

In order to meet the required format for a social security number input field using reactive forms, it must adhere to the following conditions: The format should be ###-##-####. Limited to 9 digits without including the dashes (11 characters with dashes) ...

Error in Django framework: Attempting to insert a null value in the "hardware_type" column violates the not-null constraint

I have encountered a problem for which I have found solutions on Stack Overflow, but unfortunately, they do not work in my specific case. Here is a snippet of my code: models.py hardware_used = models.CharField(max_length=20, blank=True, null=True) core ...

Obtain an instance tuple from tuple classes using TypeScript 3.0 generic rest tuples type

When it comes to retrieving the correct instance type from a class type, the process typically involves using the following code: type Constructor<T = {}> = new (...args: any[]) => T class Foo {} function getInstanceFromClass<T>(Klass: Co ...

Working with dual generic parameters in a function

Here is a function I am using: bind<T, K extends keyof T>( data: T[], bindedData: T[], key: K, newKey: string ) { } I'm trying to use two generic parameters, but my attempt here did not work: bind<T, K extends keyof T> ...

What is the most effective method for testing event emitters?

Imagine I have a basic component structured like this: @Component({ selector: 'my-test', template: '<div></div>' }) export class test { @Output selected: EventEmitter<string> = new EventEmitter<string>() ...

What is preventing React CLI from installing the template as TypeScript?

When I run npm init react-app new-app --template typescript, it only generates a Javascript template project instead of a Typescript one. How can I create a Typescript project using the CLI? Current Node JS version: 15.9.0 NPM version: 7.0.15 ...

Using Angular, you can easily add a <div> dynamically to all elements that share a class

In order to implement a solution, I am tasked with adding a new div element with the class '.cart-list-value .delete-product', which will contain a background image, to all elements that have the class .cart-list-item. Although I successfully man ...

Save the ID values of selected users in Angular Mentions feature

Using the angular mentions library, I successfully incorporated a textarea that enables me to mention multiple users. Is there a method to store the IDs of the selected users? Ideally, I would like to save them as an array of strings. For instance: If I ...

Implement the click event binding using classes in Angular 2

If I have the template below, how can I use TypeScript to bind a click event by class? My goal is to retrieve attributes of the clicked element. <ul> <li id="1" class="selectModal">First</li> <li id="2" class="selectModal">Seco ...

Issue with deploying lodash library

I'm encountering an issue with lodash. Whenever I deploy using gulp, I consistently receive the following error: vendors.min.js:3 GET http://127.0.0.1/projects/myproject/lodash 404 (Not Found) I have declared the library in my index.html file < ...

Can you provide guidance on how to access my account using the code that I have?

I'm having trouble getting the login functionality to work properly with this code. When I click the login button, nothing happens - no errors are displayed either. Can you help me identify what might be causing this issue? login() { var url = &ap ...

Exploring the process of linking a C# REST API to an Ionic 2 mobile application

I am completely lost on how to connect an asp.net web api with my ionic 2 mobile app. Any help or advice on resolving this problem would be greatly valued! ...