angular/components · error · SchematicsException
Module not found: ${modulePath}
Error message
Module not found: ${modulePath} What it means
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.
Source
Thrown at src/cdk/schematics/utils/ast.ts:55
}
/**
* Import and add module to specific module path.
* @param host the tree we are updating
* @param modulePath src location of the module to import
* @param moduleName name of module to import
* @param src src location to import
*/
export function addModuleImportToModule(
host: Tree,
modulePath: string,
moduleName: string,
src: string,
) {
const moduleSource = parseSourceFile(host, modulePath);
if (!moduleSource) {
throw new SchematicsException(`Module not found: ${modulePath}`);
}
const changes = addImportToModule(moduleSource, modulePath, moduleName, src);
const recorder = host.beginUpdate(modulePath);
changes.forEach(change => {
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}
});
host.commitUpdate(recorder);
}
/** Wraps the internal find module from options with undefined path handling */
export async function findModuleFromOptions(
host: Tree,
options: ComponentOptions,View on GitHub (pinned to 0411926e7d)
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.
Example fix
// before
addModuleImportToModule(host, 'src/app/app.module.ts', 'MatTableModule', 'src/app/app.component.ts');
// after
const modulePath = 'src/app/app.module.ts';
if (!host.exists(modulePath)) {
throw new Error(`NgModule not found at ${modulePath}; pass the real path`);
}
addModuleImportToModule(host, modulePath, 'MatTableModule', 'src/app/app.component.ts'); Defensive patterns
Strategy: validation
Validate before calling
import { Tree } from '@angular-devkit/schematics';
function assertModuleExists(host: Tree, modulePath: string): void {
if (!host.exists(modulePath)) {
throw new Error(`NgModule not found in tree: ${modulePath}`);
}
}
assertModuleExists(host, 'src/app/app.module.ts'); Type guard
function moduleFileExists(host: Tree, modulePath: string): modulePath is string {
return typeof modulePath === 'string' && modulePath.length > 0 && host.exists(modulePath);
} Try / catch
try {
addModuleImportToModule(host, modulePath, moduleName, src);
} catch (e) {
if (e instanceof SchematicsException && e.message.startsWith('Module not found:')) {
console.warn(`Skipping module import: ${e.message}`);
return;
}
throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Could not find file for path: ${path}
- Could not read Angular module file: ${modulePath}
- Could not find NgModule declaration inside: "${modulePath}"
- File ${modulePath} does not exist.
- Could not read file for path: ${htmlFilePath}
AI-assisted analysis of angular/components@0411926e7d (2026-08-31).
Data as JSON: /api/errors/d195090ced0591e0.
Report an issue: GitHub.