angular/components · error · SchematicsException
File ${modulePath} does not exist.
Error message
File ${modulePath} does not exist. What it means
readIntoSourceFile is the shared file-reading helper in build-component.ts used by the component schematics' source and addDeclarationToNgModule; if host.read(modulePath) returns null the helper throws this SchematicsException naming the missing file. It prevents TS parsing of a nonexistent file during component generation.
Source
Thrown at src/cdk/schematics/utils/build-component.ts:62
function buildDefaultPath(project: ProjectDefinition): string {
const root = project.sourceRoot ? `/${project.sourceRoot}/` : `/${project.root}/src/`;
const projectDirName =
project.extensions['projectType'] === ProjectType.Application ? 'app' : 'lib';
return `${root}${projectDirName}`;
}
/**
* List of style extensions which are CSS compatible. All supported CLI style extensions can be
* found here: angular/angular-cli/main/packages/schematics/angular/ng-new/schema.json#L118-L122
*/
const supportedCssExtensions = ['css', 'scss', 'less'];
function readIntoSourceFile(host: Tree, modulePath: string) {
const text = host.read(modulePath);
if (text === null) {
throw new SchematicsException(`File ${modulePath} does not exist.`);
}
return ts.createSourceFile(modulePath, text.toString('utf-8'), ts.ScriptTarget.Latest, true);
}
function addDeclarationToNgModule(options: ComponentOptions): Rule {
return (host: Tree) => {
if (options.skipImport || options.standalone || !options.module) {
return host;
}
const modulePath = options.module;
let source = readIntoSourceFile(host, modulePath);
const componentPath =
`/${options.path}/` +
(options.flat ? '' : strings.dasherize(options.name) + '/') +
strings.dasherize(options.name) +View on GitHub (pinned to 0411926e7d)
Solutions
- If generating into a project without an NgModule (standalone), skip module registration — newer CLI versions of the component schematic do this automatically; upgrade the library/CLI.
- Confirm the module file exists at the path the schematic derives; restore or rename accordingly.
- Run the generator from the workspace root or with the correct --project flag so paths resolve.
- Verify angular.json points to the right root/sourceRoot for the target project.
Example fix
// before (standalone app, no module) ng generate component dashboard --module src/app/app.module.ts // after (standalone: don't pass --module; component registers itself) ng generate component dashboard
Defensive patterns
Strategy: validation
Validate before calling
import { Tree } from '@angular-devkit/schematics';
function assertFileReadable(host: Tree, path: string): void {
if (host.read(path) === null) {
throw new Error(`File missing from tree: ${path}`);
}
}
assertFileReadable(host, 'src/app/app.module.ts'); Type guard
function hasFile(host: Tree, path: unknown): path is string {
return typeof path === 'string' && path.length > 0 && host.read(path) !== null;
} Try / catch
try {
const source = readIntoSourceFile(host, modulePath);
} catch (e) {
if (e instanceof SchematicsException && e.message.startsWith('File ') && e.message.endsWith('does not exist.')) {
console.warn(`Skipping NgModule registration; module file missing: ${modulePath}`);
return;
}
throw e;
} Prevention
- For standalone projects, pass --skip-module (or omit --module) when generating components.
- Verify the target module exists before running generation in multi-project workspaces.
- Use --project to point the generator at the right application.
- Keep angular.json root/sourceRoot accurate so derived paths match disk.
When it happens
Trigger: ng generate component <name> (from libraries built on this CDK schematics util) when addDeclarationToNgModule tries to read app.module.ts and it is absent or renamed, or when the source rule reads a template/stylesheet path that does not exist in the Tree.
Common situations: Standalone-first Angular 17+ projects without app.module.ts, renamed module files, generation invoked from a subdirectory so the module path resolves incorrectly, and multi-project workspaces where the schematic hardcodes the default project's module path.
Related errors
- Could not find file for path: ${path}
- Module not found: ${modulePath}
- Could not read Angular module file: ${modulePath}
- Could not read file for path: ${htmlFilePath}
- Cannot read "${filePath}" because it does not exist.
AI-assisted analysis of angular/components@0411926e7d (2026-08-31).
Data as JSON: /api/errors/59075492ce8716ae.
Report an issue: GitHub.