angular/angular-cli · error · Error
Couldn't find a route declaration in ${fileToAdd}.\nUse the
Error message
Couldn't find a route declaration in ${fileToAdd}.\nUse the '--module' option to specify a different routing module. What it means
addRouteDeclarationToModule locates the RouterModule declaration (e.g. RouterModule.forRoot/forChild) in the given source file to append a route. If the file has no RouterModule declaration, it throws with guidance to pick the right module via --module, since the schematic cannot know where routes belong.
Source
Thrown at packages/schematics/angular/utility/ast-utils.ts:606
const arrLiteral = assignment.initializer as ts.ArrayLiteralExpression;
return arrLiteral.elements
.filter((el) => el.kind === ts.SyntaxKind.CallExpression)
.find((el) => (el as ts.Identifier).getText().startsWith('RouterModule'));
}
/**
* 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)) {View on GitHub (pinned to bb72145f9a)
Solutions
- Re-run with the correct `--module` option pointing at the routing module that declares RouterModule (e.g. --module app-routing.module.ts).
- Add RouterModule import (RouterModule.forRoot([])/forChild([])) to the target module first, then re-run the schematic.
- For standalone apps, add routes via the `routes` array in provideRouter(appConfig) instead of using NgModule-based route schematics.
- Verify the file you target actually contains `RouterModule` with `grep RouterModule <file>`.
- null
Example fix
// before (target module)
@NgModule({ imports: [BrowserModule] })
// after
@NgModule({ imports: [BrowserModule, RouterModule.forRoot([])] }) Defensive patterns
Strategy: validation
Validate before calling
const src = ts.createSourceFile(file, fs.readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true);
if (!src.statements.some(s => s.getText().includes('RouterModule'))) {
console.warn(`${file} has no RouterModule declaration; use --module to pick the routing module.`);
} Type guard
function isRouterModuleCall(n: ts.Node): n is ts.CallExpression {
return ts.isCallExpression(n) && n.expression.getText().startsWith('RouterModule.');
} Try / catch
try {
await generateModule({ route: 'x', module: 'app-routing.module.ts' });
} catch (e) {
if (String(e?.message).includes("Couldn't find a route declaration")) {
console.error('Re-run with --module pointing at the module declaring RouterModule.');
}
throw e;
} Prevention
- Always pass --module with the routing module path for route-generating schematics.
- Keep RouterModule.forRoot/forChild in the standard routing module file.
- For standalone apps use provideRouter(routes) and skip NgModule route schematics.
- grep for RouterModule in the target file before running route schematics.
When it happens
Trigger: Running `ng generate module --route x` or route-adding schematics against a module file that does not import/declare RouterModule; passing the wrong --module path; standalone-style or lazily routed setups where the target file simply has no routerModuleExpr.
Common situations: Pointing --module at app.module.ts when routes live in app-routing.module.ts; newly generated modules that haven't imported RouterModule yet; standalone applications without any NgModule router declaration.
Related errors
- The router module method doesn't have arguments at line ${li
- Prerequisite for application shell is to define a router-out
- Cannot find the "provideServerRendering" function call in "$
- No route declaration array was found that corresponds to rou
- Option "project" is required.
AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30).
Data as JSON: /api/errors/33a99034f291b610.
Report an issue: GitHub.