angular/angular-cli · error · Error

No route declaration array was found that corresponds to rou

Error message

No route declaration array was found that corresponds to router module at line ${line} in ${fileToAdd}

What it means

addRouteDeclarationToModule locates the variable holding the Routes array (the second argument of forRoot/forChild, e.g. `appRoutes`) in the module source. When the RouterModule.forRoot/forChild call's argument does not correspond to any declared array variable in the file, the schematic cannot know where to append the new route and throws this error.

Source

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

  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;
      });
    }

    if (!routesVar) {
      const { line } = source.getLineAndCharacterOfPosition(routesArg.getStart());
      throw new Error(
        `No route declaration array was found that corresponds ` +
          `to router module at line ${line} in ${fileToAdd}`,
      );
    }

    routesArr = findNodes(
      routesVar,
      ts.SyntaxKind.ArrayLiteralExpression,
      1,
    )[0] as ts.ArrayLiteralExpression;
  }

  const occurrencesCount = routesArr.elements.length;
  const text = routesArr.getFullText(source);

  let route: string = routeLiteral;
  let insertPos = routesArr.elements.pos;

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Declare the routes as a named exported/const array in the same file, e.g. `const routes: Routes = [];` and pass it: `RouterModule.forRoot(routes)`.
  2. Ensure the array variable name matches what is passed to forRoot/forChild (no inline literals).
  3. Add the route manually with `@NgModule` imports edited by hand, then re-run generation with --skip-import style options where available.

Example fix

// before
@NgModule({ imports: [RouterModule.forRoot([])] })
// after
const routes: Routes = [];
@NgModule({ imports: [RouterModule.forRoot(routes)] })
Defensive patterns

Strategy: validation

Validate before calling

const src = fs.readFileSync('src/app/app.module.ts', 'utf8');
if (!/RouterModule\.for(Root|Child)\(\s*[A-Za-z_$][\w$]*\s*\)/.test(src)) {
  throw new Error('RouterModule call must reference a named routes array in the same file');
}

Prevention

When it happens

Trigger: Running `ng generate module --routing` or route-adding schematics against a module where the RouterModule.forRoot(...) / forChild(...) second argument is not a simple identifier referencing a locally declared array — e.g. it is inlined, imported from elsewhere, or the array declaration was removed.

Common situations: Hand-edited app.module.ts where `const routes: Routes = [...]` was deleted or renamed; routes defined inline as `RouterModule.forRoot([])`; routes imported from a separate file; code minified or heavily refactored before re-running the schematic.

Related errors


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