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

Creating a shared function using TypeScript

Having a vue3 component that displays a list of items and includes a function to delete an item raises a concern about checking parameters and specifying the array for the filter operation. The goal is to create a universal function using typescript. <t ...

Is there an automatic bottom padding feature?

Currently, I am facing a challenge in fitting the loader into the container without it being overridden by the browser. Using padding-bottom is not an ideal solution as it results in the loader appearing un-resized and unprofessional. Any suggestions or co ...

The browser is not displaying the HTML correctly for the Polymer Paper-Menu component

I attempted to implement a paper-menu, but I am facing issues with the rendered HTML and its interaction. When I click on a menu item, the entire list disappears due to the paper-item elements not being properly placed inside a key div within the paper-men ...

What is the best way to include the number 7 and other lower numbers in an array?

I am facing an issue where I need to retrieve all the months that have passed in 2020, such as January, February, March, April, May, June, July, and August, and store them in an array. After executing the code below, my variable 'month' returns ...

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" ...

What makes TypeScript believe that the variable could possibly be undefined when it is clearly not the case?

I recently encountered an issue where TypeScript incorrectly identifies a variable as possibly being undefined. Here is a simplified example: const func = (val1?: boolean, val2?: boolean) => { if (!val1 && !val2) return; let result: boolean; ...

Guide to creating a universal interface using the generic class concept

I am in the process of developing a new augmented, generic interface that is based on an existing interface. The original interface sets out the properties that the object will possess (root). The enhanced type should also have these properties, but instea ...

Viewing the photo container before uploading while having text overlap

I'm encountering an issue where the image previews are overlapping with the text in another div. Here are the screenshots: the first one shows how it looks before the preview, and the second one shows what happens when images are added: https://i.sst ...

Expanding interfaces dynamically in Typescript

Currently, I am facing a challenge while attempting to integrate an existing React Native module equipped with the following props: useComponent1: boolean useComponent2: boolean This is how the implementation looks like: render(){ if(useComponent1){ ...

Using an array to enforce form validation rules in Angular, including prohibited form values

I am currently developing a basic Angular form with specific validation requirements: The input must not be left empty The input cannot match any of the values stored in the array forbiddenValues. While I understand how to implement the required validat ...

Enhance your FullCalendar experience with React by displaying extra information on your calendar

I am new to using React and FullCalendar, and I have a page layout similar to the image linked below. https://i.sstatic.net/MooTR.png Additionally, I have a list of events structured as shown: id: "9", eventId: "1", ...

An interface that is extended by an optional generic parameter

I currently have an exported abstract class that has one generic. However, I now require two generics. I do not want to modify all existing classes that are using this class. Therefore, I am looking to add an optional generic class that extends an interfac ...

Tips for incorporating Material UI Icon v1.0.0-beta.36 into a .tsx component

Currently utilizing material-ui-icons v1.0.0-beta.36. I am endeavoring to incorporate a Search icon within a .tsx component. .tsx component: import React, { Component, ReactElement } from 'react' import Search from 'material-ui-icons/Sear ...

Setting up ESLint and Prettier for Accurate Error Detection in TypeScript and Next.js Development

As I work with TypeScript and Next.js, I decided to implement strict code formatting rules by adding the following configuration to my eslintrc.json file: "rules": { "prettier/prettier": "error" } However, when I ran npm ru ...

The overlay pop-up does not reposition on scroll movements

Within the image, there is a MatDialog Box positioned on top of another dialog box. Inside the MatDialog Box, there is a Component called MyNewDetailComponent. This component is accessed via Overlay with the scrollOptions set to reposition(). The issue ar ...

Steps to Export Several Fusion Charts into Individual Image Files

My webpage contains multiple charts created using the Fusion Chart library. There are three different charts on the page, and I want to export each chart as a separate PNG file. However, when I click the export button, it generates separate images of the ...

What are the steps to troubleshoot a Node Package Manager library in Visual Studio Code?

I have created a Typescript library that I plan to use in various NodeJS projects. The source code is included in the NPM package, so when I install it in my projects, the source also gets added to the node_modules folder. Now, during debugging, I want to ...

The maximum nested function level has been reached at tsc:1. Consider increasing the FUNCNEST limit

Having an issue while trying to compile a typescript file, encountering the following error: work/gigMax [typescriptMigration●] » tsc src/mutate.ts tsc:1: maximum nested function level reached; increase FUNCNEST? work/gigMax [typescriptMigration●] ...

Error encountered: NextJs could not find the specified module, which includes Typescript and SCSS

I am in the process of migrating a Next.js application from .js to .ts and incorporating ScSS. The first error I encounter is during 'npm run dev'. However, when I try 'npm run build', different issues arise that do not seem related to ...

Transform a row in an ng Smart table to a routerlink using Angular 2

I've been exploring ng2 Smart Table and I'm looking to convert a row (or even cell data) into a clickable link using routerlink. The current method I'm employing to retrieve some of my row's data is as follows: onUserRowSelect(event) ...