angular/angular-cli · error · Error
Module option required when creating a lazy loaded routing m
Error message
Module option required when creating a lazy loaded routing module.
What it means
The module schematic's route-adding rule requires the `--module` option when `--route` is supplied for a lazy-loaded routing module. Without `--module` it cannot determine which NgModule (or standalone route file) the route declaration should be added to, so it throws.
Source
Thrown at packages/schematics/angular/module/index.ts:93
recorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(recorder);
return host;
};
}
function addRouteDeclarationToNgModule(
options: ModuleOptions,
routingModulePath: string | undefined,
): Rule {
return (host: Tree) => {
if (!options.route) {
return host;
}
if (!options.module) {
throw new Error('Module option required when creating a lazy loaded routing module.');
}
let path: string;
if (routingModulePath) {
path = routingModulePath;
} else {
path = options.module;
}
const sourceText = host.readText(path);
const addDeclaration = addRouteDeclarationToModule(
ts.createSourceFile(path, sourceText, ts.ScriptTarget.Latest, true),
path,
buildRoute(options, options.module),
) as InsertChange;
const recorder = host.beginUpdate(path);View on GitHub (pinned to bb72145f9a)
Solutions
- Re-run with --module, e.g. `ng generate module users --route users --module app.module.ts`.
- Pass `--module=app` (the module name resolves to its path) when the routing parent is AppModule.
- If the feature is standalone, drop `--route` and use `ng generate module` without lazy-route options, or use `ng g @angular/core:route` style workflows.
- In code, set options.module before invoking the Rule.
Example fix
// before ng g module admin --route admin // after ng g module admin --route admin --module app.module.ts
Defensive patterns
Strategy: validation
Validate before calling
if (options.route && !options.module) {
throw new Error('Pass --module (e.g. --module=app.module.ts) when using --route.');
} Try / catch
try {
await generateModule({ route: 'admin', module: 'app.module.ts' });
} catch (e) {
if (e.message.includes('Module option required')) {
console.error('Add the --module flag when generating a lazy routed module.');
} else throw e;
} Prevention
- Always pair --route with --module when generating lazy modules
- Use CLI prompts interactively so missing options are requested
- Codify generation flags in npm scripts to keep them consistent
- For standalone apps, prefer router file-based or `ng g @angular/core:route` flows
When it happens
Trigger: Calling the module schematic (e.g. `ng generate module foo --route foo --module ???` omitted) with options { route: 'foo', module: undefined } — i.e. `ng g m users --route users` without `--module=app`.
Common situations: Developers forget `--module` when generating lazy-loaded feature modules; scripted generation templates omit the module flag; interactive prompt skipped in CI.
Related errors
- The specified include path '${options.include}' does not exi
- No route declaration array was found that corresponds to rou
- Could not find a non Routing NgModule.\nModules with suffix
- Option "project" is required.
- Project is not defined in this workspace.
AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30).
Data as JSON: /api/errors/879c228674d1bcc4.
Report an issue: GitHub.