angular/angular-cli · error · Error

The router module method doesn't have arguments at line ${li

Error message

The router module method doesn't have arguments at line ${line} in ${fileToAdd}

What it means

After finding the RouterModule declaration, the schematic reads its call arguments (the first should be the routes array). If the RouterModule call expression has zero arguments (e.g. bare `RouterModule.forRoot()` or a non-call reference), there is nowhere to insert the route and it throws, reporting the line number for debugging.

Source

Thrown at packages/schematics/angular/utility/ast-utils.ts:614

/**
 * Adds a new route declaration to a router module (i.e. has a RouterModule declaration)
 */
export function addRouteDeclarationToModule(
  source: ts.SourceFile,
  fileToAdd: string,
  routeLiteral: string,
): Change {
  const routerModuleExpr = getRouterModuleDeclaration(source);
  if (!routerModuleExpr) {
    throw new Error(
      `Couldn't find a route declaration in ${fileToAdd}.\n` +
        `Use the '--module' option to specify a different routing module.`,
    );
  }
  const scopeConfigMethodArgs = (routerModuleExpr as ts.CallExpression).arguments;
  if (!scopeConfigMethodArgs.length) {
    const { line } = source.getLineAndCharacterOfPosition(routerModuleExpr.getStart());
    throw new Error(
      `The router module method doesn't have arguments ` + `at line ${line} in ${fileToAdd}`,
    );
  }

  let routesArr: ts.ArrayLiteralExpression | undefined;
  const routesArg = scopeConfigMethodArgs[0];

  // Check if the route declarations array is
  // an inlined argument of RouterModule or a standalone variable
  if (ts.isArrayLiteralExpression(routesArg)) {
    routesArr = routesArg;
  } else {
    const routesVarName = routesArg.getText();
    let routesVar;
    if (routesArg.kind === ts.SyntaxKind.Identifier) {
      routesVar = source.statements.filter(ts.isVariableStatement).find((v) => {
        return v.declarationList.declarations[0].name.getText() === routesVarName;
      });

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Add an empty routes array to the call: change RouterModule.forRoot() to RouterModule.forRoot([]), then re-run the schematic.
  2. Extract the routes to a constant (e.g. const routes: Routes = []) and pass it: RouterModule.forRoot(routes).
  3. Point --module at the module whose RouterModule call actually receives the routes array.
  4. Move route registration to provideRouter(routes) for standalone apps instead of the empty NgModule call.
  5. null

Example fix

// before
imports: [RouterModule.forRoot()]
// after
imports: [RouterModule.forRoot([])]
Defensive patterns

Strategy: validation

Validate before calling

const src = ts.createSourceFile(file, fs.readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true);
const rm = src.statements.find(s => s.getText().includes('RouterModule.forRoot')) as ts.CallExpression | undefined;
if (!rm || (rm.arguments?.length ?? 0) === 0) {
  console.warn(`RouterModule call in ${file} has no arguments; add a routes array first.`);
}

Type guard

function hasRoutesArg(n: ts.Node): n is ts.CallExpression & { arguments: [ts.Expression, ...ts.Expression[]] } {
  return ts.isCallExpression(n) && n.arguments.length > 0;
}

Try / catch

try {
  await generateRouteSchematic();
} catch (e) {
  if (String(e?.message).includes("doesn't have arguments")) {
    console.error('Change RouterModule.forRoot() to RouterModule.forRoot([]) and re-run.');
  }
  throw e;
}

Prevention

When it happens

Trigger: RouterModule declared as `RouterModule.forRoot()` / `forChild()` with no arguments, or `RouterModule` referenced without a call expression so `.arguments` is empty; running route-add schematics against such a module file.

Common situations: Placeholder empty forRoot() calls created manually; route config moved to a separate file leaving an empty forRoot(); copy-pasted module skeletons without the routes array.

Related errors


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