{"record":{"id":"33a99034f291b610","repo":"angular/angular-cli","slug":"couldn-t-find-a-route-declaration-in-filetoadd","errorCode":null,"errorMessage":"Couldn't find a route declaration in ${fileToAdd}.\\nUse the '--module' option to specify a different routing module.","messagePattern":"Couldn't find a route declaration in (.+?)\\.\\\\nUse the '--module' option to specify a different routing module\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/schematics/angular/utility/ast-utils.ts","lineNumber":606,"sourceCode":"\n  const arrLiteral = assignment.initializer as ts.ArrayLiteralExpression;\n\n  return arrLiteral.elements\n    .filter((el) => el.kind === ts.SyntaxKind.CallExpression)\n    .find((el) => (el as ts.Identifier).getText().startsWith('RouterModule'));\n}\n\n/**\n * Adds a new route declaration to a router module (i.e. has a RouterModule declaration)\n */\nexport function addRouteDeclarationToModule(\n  source: ts.SourceFile,\n  fileToAdd: string,\n  routeLiteral: string,\n): Change {\n  const routerModuleExpr = getRouterModuleDeclaration(source);\n  if (!routerModuleExpr) {\n    throw new Error(\n      `Couldn't find a route declaration in ${fileToAdd}.\\n` +\n        `Use the '--module' option to specify a different routing module.`,\n    );\n  }\n  const scopeConfigMethodArgs = (routerModuleExpr as ts.CallExpression).arguments;\n  if (!scopeConfigMethodArgs.length) {\n    const { line } = source.getLineAndCharacterOfPosition(routerModuleExpr.getStart());\n    throw new Error(\n      `The router module method doesn't have arguments ` + `at line ${line} in ${fileToAdd}`,\n    );\n  }\n\n  let routesArr: ts.ArrayLiteralExpression | undefined;\n  const routesArg = scopeConfigMethodArgs[0];\n\n  // Check if the route declarations array is\n  // an inlined argument of RouterModule or a standalone variable\n  if (ts.isArrayLiteralExpression(routesArg)) {","sourceCodeStart":588,"sourceCodeEnd":624,"githubUrl":"https://github.com/angular/angular-cli/blob/bb72145f9ab45aee29f523236b3a25cd0813a841/packages/schematics/angular/utility/ast-utils.ts#L588-L624","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before (target module)\n@NgModule({ imports: [BrowserModule] })\n// after\n@NgModule({ imports: [BrowserModule, RouterModule.forRoot([])] })","handlingStrategy":"validation","validationCode":"const src = ts.createSourceFile(file, fs.readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true);\nif (!src.statements.some(s => s.getText().includes('RouterModule'))) {\n  console.warn(`${file} has no RouterModule declaration; use --module to pick the routing module.`);\n}","typeGuard":"function isRouterModuleCall(n: ts.Node): n is ts.CallExpression {\n  return ts.isCallExpression(n) && n.expression.getText().startsWith('RouterModule.');\n}","tryCatchPattern":"try {\n  await generateModule({ route: 'x', module: 'app-routing.module.ts' });\n} catch (e) {\n  if (String(e?.message).includes(\"Couldn't find a route declaration\")) {\n    console.error('Re-run with --module pointing at the module declaring RouterModule.');\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["angular","router","schematics","ast","module"],"backgroundTag":"no-router-module-declaration","analyzedSha":"bb72145f9ab45aee29f523236b3a25cd0813a841","analyzedAt":"2026-08-30T02:47:34.745Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}