Lazy-loading modules in SSR Angular 8 applications are currently unspecified

I am currently in the process of setting up my Angular 8 application to work with server-side rendering (SSR). However, I am encountering some undefined errors in webpack when running my application using ng serve, especially with lazy-loaded modules. Everything was functioning properly when I was using Angular 7, but after migrating to Angular 8, everything seems to be broken.

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'call' of undefined
TypeError: Cannot read property 'call' of undefined
    at __webpack_require__ (/Users/gwenaellarmet/Documents/Projects/ng-nouma/dist/server.js:137403:30)
    at Function.requireEnsure [as e] (/Users/gwenaellarmet/Documents/Projects/ng-nouma/dist/server.js:137422:25)
    at ɵ11 (/Users/gwenaellarmet/Documents/Projects/ng-nouma/dist/server.js:139660:6210)
    at RouterConfigLoader.loadModuleFactory (/Users/gwenaellarmet/Documents/Projects/ng-nouma/dist/server.js:232758:39)

I have made adjustments to both the tsconfig and angular.json configurations, but I keep encountering the same error consistently. I have disabled Ivy as it was also causing issues during the build process. While I suspect a connection between these problems, I cannot confirm it at this point, so my focus is on getting things to work without Ivy.

ERROR in src/app/pages/search/search.module.ts(68,12): error TS-991010: Value at position 12 in the NgModule.importss of SearchModule is not a reference: [object Object]
src/app/app.module.ts(41,12): error TS-991010: Value at position 6 in the NgModule.importss of AppModule is not a reference: [object Object]

tsconfig.json

... (omitted for brevity)

I am compiling my application using npm run build:ssr and serving it with npm run serve:ssr. My main goal is to ensure that all modules and markup are rendered with SSR for SEO purposes. Any assistance in pinpointing the root cause of these issues would be greatly appreciated.

Answer №1

One issue arises from having two copies of Angular in the traditional Universal setup, leading to confusion within the lazy loading system.

To resolve this problem manually, follow these steps:

Modify package.json

Add --bundleDependencies all at the end of the value for build:client-and-server-bundles

Adjust server.ts

Delete the following lines

import {enableProdMode} from '@angular/core';     
import {ngExpressEngine} from '@nguniversal/express-engine';  
import {provideModuleMap} from '@nguniversal/module-map-ngfactory-loader';
enableProdMode();
const {AppServerModuleNgFactory, LAZY_MODULE_MAP} = require('./dist/server/main');

Then add

const {AppServerModuleNgFactory, LAZY_MODULE_MAP, ngExpressEngine, provideModuleMap} = require('./dist/server/main');

Update main.server.ts

Include

export {ngExpressEngine} from '@nguniversal/express-engine';
export {provideModuleMap} from '@nguniversal/module-map-ngfactory-loader';

Edit webpack.server.config.js

Insert

externals: {
 './dist/server/main': 'require("./server/main")'
}

For further details, check out: fix lazy loading and bundleDependencies

Answer №3

In short, I successfully got my project to work by simply removing the line enableProdMode(); from server.ts.


While this code was necessary for Angular 7, it seems that Angular 8 required some changes in the server.ts file.

  • To be safe, I created a backup file called server.bak.ts,
  • I then started a new blank project with SSR,
  • and finally copied the necessary code from the new project into the server.ts file.

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

What is the best way to emphasize when the path matches exactly with '/'?

Is there a way to highlight the path only when it exactly matches '/'? Currently, even on 'Page 2', the 'Home' link is still highlighted. Check out the plunker here .active { color: red; } <a routerLinkActive="active" r ...

Make sure to enclose the useSearchParams() function within a suspense boundary specifically for the page at "/" location

An error popped up during the build process, displaying the messages: useSearchParams() should be enclosed within a suspense boundary at page "/404". For more information, check out: https://nextjs.org/docs/messages/missing-suspense-with-csr-bailout Error ...

Sending user input data from a React text field to a function as arguments

Below are input fields, from which I need to retrieve the entered values and pass them to the onClick event of the button displayed below. <input type="text" style={textFieldStyle} name="topicBox" placeholder="Enter topic here..."/> <input type=" ...

Best practices for customizing Material UI v5 Switch using Theme

I've successfully implemented a Material Switch based on my design by creating a custom component and styling it using the styled and sx prop. However, I'm interested in figuring out how to achieve the same result within the theme itself so that ...

The process of retrieving input field values from a table within an Angular reactive form

I am seeking a way to collect all input values found within the table rows inside a reactive form. Below is an explanation of my code. <form [formGroup]="productForm" (ngSubmit)="saveProduct($event)" novalidate> <mat-form-field appearance="outlin ...

Changing the content of an HTTP response with the help of RXJS

My API response includes a payload with various details about the queue and its customers. Here is an example of the payload: { "uid": "string", "queue": { "size": 0, "averageWait ...

Creating a Redis client in Typescript using the `redis.createClient()` function

I'm currently trying to integrate Redis (v4.0.1) into my Express server using TypeScript, but I've encountered a small issue. As I am still in the process of learning TypeScript, I keep getting red underline errors on the host parameter within th ...

What is the reason behind the input text field not preserving its value when disabled?

Currently, I am working on a basic input field that utilizes Angular within StorybookJS. However, I have encountered an issue where the input text field does not maintain its value when the disabled attribute is set to true. For example, if I manually typ ...

Exclude the initial argument from functions listed within a JSON structure

Is there a way to create a generic type that reflects a JSON object structure, but excludes the first argument from functions while maintaining the types of other arguments? type InputType<T> = { increment: (state: T) => T, add: (state: T, cou ...

Creating a Tailored Validation Function in Angular 6

Looking to develop a custom generic validator that accepts the regular expression pattern and the property name (from a formgroup) as parameters? Take a look at this code snippet: UserName: new FormControl('', [ Validators.require ...

Error: Unable to install Angular on WSL due to permission denied for Node

Struggling to set up angular on WSL2, with node version 17.3.1 and npm version 8.3.0 already installed. Received an error when trying the command npm install -g @angular/cli: npm ERR! code 127 npm ERR! path /root/.nvm/versions/node/v17.3.1/lib/node_module ...

What is the process for setting a push key while pushing data to a Firebase database?

When writing data to the Firebase database using both Angular 4 on the frontend and Firebase functions on the backend, a unique push key is generated by Firebase. This key makes it difficult to access the data in the future. I am curious if there is a way ...

Tips for Implementing a Button Click Sound in Ionic 2

I've noticed that many native apps have a distinct click sound when buttons are pressed. Is there a way for me to add this same feature to all buttons in Ionic 2? ...

SweetAlert2 not displaying properly in Ionic6 - troubleshooting the issue

My current project is an Ionic 5 Angular project with SweetAlerts2 popups. Recently, I decided to upgrade to Ionic6 and encountered an issue where the SweetAlerts2 popups are not displaying correctly. The alert seems to only show up in the header, leaving ...

Problems with Angular 14 and NGRX Selector causing complications

At my basket store, I currently have 5 items with 2 of them having an id of 4. https://i.stack.imgur.com/YIplp.png In my reducer, when I use: on(getBasket, (state, { id }) => state.filter((photo) => photo.id !== id) It works to remove the item fr ...

Creating a Modern Application with MEAN stack utilizing Angular 2 and Angular-CLI

Currently, I am in the process of developing a MEAN app using Angular 2 and Angular CLI for building. Everything seems to be running smoothly as my GitHub repository can attest (link here). However, upon trying to access the page, I encounter multiple refe ...

The async validator in Angular will only activate upon submission if the user has made changes to the form

In my angular 10 application, I have implemented a login form with various validation checks for the username and password fields. These validations are carried out using an async validator that is applied to the formgroup containing the username and pass ...

The XMLHttpRequest response states that the preflight request did not meet the access control check requirements

I am attempting to subscribe to a firebase cloud messaging topic by sending an http post request. var data = null; var xhr = new XMLHttpRequest(); xhr.withCredentials = true; xhr.addEventListener("readystatechange", function () { if (this.readyState ...

Angular 5: Issues with retrieving response using HttpClient's get request

Alright, so typically I work with Angular 1.*, but I decided to dive into Angular 5 and man, it's been a bit of a challenge. It feels unnecessarily complex, but oh well... So I'm trying to make an HTTP call, and I have this node API that is retu ...

What is the method for retrieving the index of an enum member in Typescript, rather than the member name?

Here is an example of how to work with enums in TypeScript: export enum Category { Action = 1, Option = 2, RealEstateFund = 3, FuturesContract = 4, ETFs = 5, BDRs = 6 } The following function can be used to retrieve the enum indexe ...