Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Angular Lazy Loading Does Not Work in Production Mode

Before asking this question, I read all the similar questions in StackOverflow and tried all the solutions mentioned by other users but still stuck in this seemingly simple problem. Everything works fine in my local MacBook Air M1 while developing but after producing the production build it is not working in my Windows Server 2019 Standard which is my production server. The following is the simple version of my source code.

AppModule:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

AppRoutingModule:

import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";

const routes: Routes = [
  { path: "", pathMatch: "full", redirectTo: "/test" },
  {
    path: "test",
    loadChildren: () =>
      import("@project-name/test").then((m) => m.TestModule),
  },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
})
export class AppRoutingModule {}

app.component.html:

<h1>I am app component!</h1>
<router-outlet></router-outlet>

TestModule:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { TestComponent } from './test.component';

@NgModule({
  imports: [CommonModule, RouterModule.forChild([{ path: '', component: TestComponent }])],
  declarations: [TestComponent]
})
export class TestModule {}

TestComponent:

<h1>I am a test component!</h1>

In production server, when I hit the localhost/my-project I will the test component successfully loaded; but when I hit the localhost/my-project/test I see 404 Not Found and nothing is shown in the browser. I should mention that I have hosted the built app on nginx/1.19.10 and I am developing the app using Nx monorepo.

What I have tried so far but the problem not solved yet:

  • Checking import paths to be consistent and not different in the project (e.g., "@project-name/test")
  • Setting tsconfig.compilerOptions to esnext
  • Turning of the build optimiser and aot using both ng build --prod --build-optimizer=false --aot=false command and configuration section in the angular.json
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!