{"record":{"id":"9f9320ca533f8835","repo":"angular/components","slug":"could-not-find-ngmodule-declaration-inside-mod","errorCode":null,"errorMessage":"Could not find NgModule declaration inside: \"${modulePath}\"","messagePattern":"Could not find NgModule declaration inside: \"(.+?)\"","errorType":"exception","errorClass":"SchematicsException","httpStatus":null,"severity":"error","filePath":"src/cdk/schematics/utils/ast/ng-module-imports.ts","lineNumber":31,"sourceCode":" * Whether the Angular module in the given path imports the specified module class name.\n */\nexport function hasNgModuleImport(tree: Tree, modulePath: string, className: string): boolean {\n  const moduleFileContent = tree.read(modulePath);\n\n  if (!moduleFileContent) {\n    throw new SchematicsException(`Could not read Angular module file: ${modulePath}`);\n  }\n\n  const parsedFile = ts.createSourceFile(\n    modulePath,\n    moduleFileContent.toString(),\n    ts.ScriptTarget.Latest,\n    true,\n  );\n  const ngModuleMetadata = findNgModuleMetadata(parsedFile);\n\n  if (!ngModuleMetadata) {\n    throw new SchematicsException(`Could not find NgModule declaration inside: \"${modulePath}\"`);\n  }\n\n  for (let property of ngModuleMetadata!.properties) {\n    if (\n      !ts.isPropertyAssignment(property) ||\n      property.name.getText() !== 'imports' ||\n      !ts.isArrayLiteralExpression(property.initializer)\n    ) {\n      continue;\n    }\n\n    if (property.initializer.elements.some(element => element.getText() === className)) {\n      return true;\n    }\n  }\n\n  return false;\n}","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/cdk/schematics/utils/ast/ng-module-imports.ts#L13-L49","documentation":"The module file was read and parsed, but findNgModuleMetadata could not locate an @NgModule(...) decorator expression in it, so hasNgModuleImport throws this SchematicsException. The file exists but is not a NgModule declaration file.","triggerScenarios":"hasNgModuleImport(tree, modulePath, className) pointed at a TS file that has no @NgModule decorator: a standalone component/directive file, a barrel index.ts, a module with a typo'd decorator import, or a file where @NgModule is aliased.","commonSituations":"Migrating to Angular 14+ standalone APIs where app.module.ts was deleted in favor of main.ts bootstrapApplication, but older schematic setup rules still reference it; accidentally passing a routing.module.ts that only declares routes; passing index.ts re-export files.","solutions":["Pass the file that actually contains @NgModule({ imports: [...] }); search the project for '@NgModule' if unsure.","If the app is standalone, replace the NgModule check: add the module's exports to the component's imports array and skip hasNgModuleImport.","Ensure @NgModule is imported from '@angular/core' in the target file (aliased decorators won't be found).","Use the project's root module path from angular.json/workspace config instead of a hardcoded guess."],"exampleFix":"// before\nhasNgModuleImport(tree, 'src/app/index.ts', 'MatDialogModule');\n// after\nhasNgModuleImport(tree, 'src/app/app.module.ts', 'MatDialogModule'); // file containing @NgModule","handlingStrategy":"type-guard","validationCode":"import * as ts from 'typescript';\nfunction containsNgModule(content: string): boolean {\n  return /@NgModule\\s*\\(/.test(content);\n}\nconst content = tree.read(modulePath)?.toString('utf-8') ?? '';\nif (!containsNgModule(content)) {\n  throw new Error(`${modulePath} has no @NgModule; pick the root module`);\n}","typeGuard":"function isNgModuleFile(tree: Tree, modulePath: string): boolean {\n  const content = tree.exists(modulePath) ? tree.read(modulePath)!.toString('utf-8') : '';\n  return /@NgModule\\s*\\(/.test(content);\n}","tryCatchPattern":"try {\n  hasNgModuleImport(tree, modulePath, className);\n} catch (e) {\n  if (e instanceof SchematicsException && e.message.includes('Could not find NgModule declaration')) {\n    console.warn(`${modulePath} is not an NgModule (standalone app or wrong file)`);\n    return false;\n  }\n  throw e;\n}","preventionTips":["Regex-check the file for @NgModule before parsing it as a module.","Never pass index.ts barrels or routing-only files to module-inspection utilities.","Detect standalone apps (bootstrapApplication in main.ts) and skip NgModule logic.","Track decorator imports: @NgModule must be imported from @angular/core, unaliased."],"tags":["angular","schematics","ngmodule","standalone"],"backgroundTag":"ngmodule-declaration-not-found","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}