angular/angular-cli · error · Error
No route declaration array was found that corresponds to…
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.
Solutions
- Declare the routes as a named exported/const array in the same file, e.g. `const routes: Routes = [];` and pass it: `RouterModule.forRoot(routes)`.
- Ensure the array variable name matches what is passed to forRoot/forChild (no inline literals).
- 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
- Always declare routes as a named const array passed to forRoot/forChild, never inline literals.
- Keep the routes array declaration in the same module file as the forRoot/forChild call.
- Avoid renaming/deleting the routes variable without updating the module.
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
- Cannot find the "provideServerRendering" function call in
- Could not find a non Routing NgModule.\nModules with suffix
- Couldn't find a route declaration in
- Module option required when creating a lazy loaded routing…
- The router module method doesn't have arguments at line
AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30).
Data as JSON: /api/errors/5fac97f0c110a362.
Report an issue: GitHub.
Appendix: 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)