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

  1. Re-run with --module, e.g. `ng generate module users --route users --module app.module.ts`.
  2. Pass `--module=app` (the module name resolves to its path) when the routing parent is AppModule.
  3. 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.
  4. 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

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


AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30). Data as JSON: /api/errors/879c228674d1bcc4. Report an issue: GitHub.