2

I have been trying for a while to get a lazy loaded module to work in a simple AOT + Webpack Angular 2 setup.

Here are my core settings, if anything else is useful to create more context please let me know and I will update the question.

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": false,
    "removeComments": true,
    "noLib": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["dom", "es6"],
    "sourceMap": true,
    "pretty": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitUseStrict": false,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "outDir": "build/tmp",
    "typeRoots": [
      "./node_modules/@types"
    ],
    "types": [
      "node",
      "jasmine"
    ]
  },
  "exclude": [
    "build",
    "dist",
    "node_modules",
    "src/main.aot.ts",
    "tmp"
  ],
  "angularCompilerOptions": {
    "debug": true,
    "genDir": "build",
    "skipMetadataEmit": true
  },
  "compileOnSave": false,
  "buildOnSave": false
}

app.routing.ts

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

export const routes: Routes = [
  { path: 'lazy', loadChildren: './sections/lazy/lazy.module#RecipesModule' },
];

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

webpack.prod.js

// ...
module: {
  rules: [
    {
      test: /\.ts$/,
      loader: '@ngtools/webpack',
    }
  ]
}
// ...
plugins: [
  // ...
  new AotPlugin({
    tsConfigPath: './tsconfig.json',
    entryModule: helpers.root('src/app/app.module#AppModule')
  }),
  // ...
]
// ...

The build process completes without any problem but the error Error: Uncaught (in promise): Error: Cannot find module './sections/lazy/lazy.module.ngfactory'. occurs when I try to navigate to the /lazy url.

Also running the app in development/JIT works without any issue.

Do you spot any problem?

Thanks

2
  • same issue with our sample repo here. There's also a branch where the JiT entry point is used instead of AoT one. No luck in both cases. Commented May 10, 2017 at 16:02
  • 1
    Hi @superjos I posted an answer that will probably solve your issue too since you're using that same plugin in your webpack configuration, hope it helps Commented May 11, 2017 at 10:25

1 Answer 1

2

I solved the problem by removing ContextReplacementPlugin I was using on my webpack build.

I had the following plugin entry in my webpack.config file that was causing the issue:

// Workaround for angular/angular#11580
new ContextReplacementPlugin(
  // The (\\|\/) piece accounts for path separators in *nix and Windows
  // https://github.com/angular/angular.io/issues/3514
  /angular(\\|\/)core(\\|\/)@angular/,
  helpers.root('src'), // location of your src
  {} // a map of your routes
),

My project configuration was based on the angular-starter repo and I was probably not using the ContextReplacementPlugin plugin correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for input. I tried on that project, commenting out that plugin entry, but nothing changed. Still hitting Error: Cannot find module '../private/module.ngfactory'.. Not sure how that can help, though: for angular projects it is suggested all over the place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.