{"record":{"id":"d195090ced0591e0","repo":"angular/components","slug":"module-not-found-modulepath","errorCode":null,"errorMessage":"Module not found: ${modulePath}","messagePattern":"Module not found: (.+?)","errorType":"exception","errorClass":"SchematicsException","httpStatus":null,"severity":"error","filePath":"src/cdk/schematics/utils/ast.ts","lineNumber":55,"sourceCode":"}\n\n/**\n * Import and add module to specific module path.\n * @param host the tree we are updating\n * @param modulePath src location of the module to import\n * @param moduleName name of module to import\n * @param src src location to import\n */\nexport function addModuleImportToModule(\n  host: Tree,\n  modulePath: string,\n  moduleName: string,\n  src: string,\n) {\n  const moduleSource = parseSourceFile(host, modulePath);\n\n  if (!moduleSource) {\n    throw new SchematicsException(`Module not found: ${modulePath}`);\n  }\n\n  const changes = addImportToModule(moduleSource, modulePath, moduleName, src);\n  const recorder = host.beginUpdate(modulePath);\n\n  changes.forEach(change => {\n    if (change instanceof InsertChange) {\n      recorder.insertLeft(change.pos, change.toAdd);\n    }\n  });\n\n  host.commitUpdate(recorder);\n}\n\n/** Wraps the internal find module from options with undefined path handling  */\nexport async function findModuleFromOptions(\n  host: Tree,\n  options: ComponentOptions,","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/cdk/schematics/utils/ast.ts#L37-L73","documentation":"addModuleImportToModule parses the target NgModule file via parseSourceFile(host, modulePath); when the file cannot be resolved in the schematic host tree it throws this SchematicsException instead of failing later with a cryptic parse error. It is a guard so the module-add rule aborts with a clear path name.","triggerScenarios":"Calling addModuleImportToModule (directly or via addDragDropModulesToModule, addFormModulesToModule, addNavModulesToModule, addTableModulesToModule, addTreeModulesToModule, addModuleImportToRootModule) with a modulePath that does not exist in the virtual Tree, e.g. a wrong app.module.ts path or a path relative to the wrong root.","commonSituations":"Running `ng add`/`ng generate` in a workspace where the app module was renamed or moved (e.g. src/app/app.module.ts relocated), Nx/multi-project layouts where the working directory is not the project root, standalone-component apps (Angular 17+) that no longer have an NgModule, or monorepos where the schematic resolves modulePath against the wrong project.","solutions":["Verify the modulePath passed (or configured) exists in the project; print it and check with ls.","If the app uses standalone bootstrap (no NgModule), this schematic path does not apply — import the module's components/directives directly in the component imports array instead.","For multi-project workspaces, run the schematic from the correct directory or pass the correct --project so the module path resolves.","Recreate or restore the missing app.module.ts, or update the schematic call site to point at the actual NgModule file path."],"exampleFix":"// before\naddModuleImportToModule(host, 'src/app/app.module.ts', 'MatTableModule', 'src/app/app.component.ts');\n// after\nconst modulePath = 'src/app/app.module.ts';\nif (!host.exists(modulePath)) {\n  throw new Error(`NgModule not found at ${modulePath}; pass the real path`);\n}\naddModuleImportToModule(host, modulePath, 'MatTableModule', 'src/app/app.component.ts');","handlingStrategy":"validation","validationCode":"import { Tree } from '@angular-devkit/schematics';\nfunction assertModuleExists(host: Tree, modulePath: string): void {\n  if (!host.exists(modulePath)) {\n    throw new Error(`NgModule not found in tree: ${modulePath}`);\n  }\n}\nassertModuleExists(host, 'src/app/app.module.ts');","typeGuard":"function moduleFileExists(host: Tree, modulePath: string): modulePath is string {\n  return typeof modulePath === 'string' && modulePath.length > 0 && host.exists(modulePath);\n}","tryCatchPattern":"try {\n  addModuleImportToModule(host, modulePath, moduleName, src);\n} catch (e) {\n  if (e instanceof SchematicsException && e.message.startsWith('Module not found:')) {\n    console.warn(`Skipping module import: ${e.message}`);\n    return;\n  }\n  throw e;\n}","preventionTips":["Call host.exists(modulePath) before any module-import utility.","Derive module paths from the workspace/project definition instead of hardcoding src/app/app.module.ts.","Handle standalone apps (no NgModule) with a separate code path.","Run schematics from the workspace root so relative paths resolve."],"tags":["angular","schematics","file-not-found","ngmodule"],"backgroundTag":"module-file-not-found","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}