{"record":{"id":"59075492ce8716ae","repo":"angular/components","slug":"file-modulepath-does-not-exist","errorCode":null,"errorMessage":"File ${modulePath} does not exist.","messagePattern":"File (.+?) does not exist\\.","errorType":"exception","errorClass":"SchematicsException","httpStatus":null,"severity":"error","filePath":"src/cdk/schematics/utils/build-component.ts","lineNumber":62,"sourceCode":"function buildDefaultPath(project: ProjectDefinition): string {\n  const root = project.sourceRoot ? `/${project.sourceRoot}/` : `/${project.root}/src/`;\n\n  const projectDirName =\n    project.extensions['projectType'] === ProjectType.Application ? 'app' : 'lib';\n\n  return `${root}${projectDirName}`;\n}\n\n/**\n * List of style extensions which are CSS compatible. All supported CLI style extensions can be\n * found here: angular/angular-cli/main/packages/schematics/angular/ng-new/schema.json#L118-L122\n */\nconst supportedCssExtensions = ['css', 'scss', 'less'];\n\nfunction readIntoSourceFile(host: Tree, modulePath: string) {\n  const text = host.read(modulePath);\n  if (text === null) {\n    throw new SchematicsException(`File ${modulePath} does not exist.`);\n  }\n\n  return ts.createSourceFile(modulePath, text.toString('utf-8'), ts.ScriptTarget.Latest, true);\n}\n\nfunction addDeclarationToNgModule(options: ComponentOptions): Rule {\n  return (host: Tree) => {\n    if (options.skipImport || options.standalone || !options.module) {\n      return host;\n    }\n\n    const modulePath = options.module;\n    let source = readIntoSourceFile(host, modulePath);\n\n    const componentPath =\n      `/${options.path}/` +\n      (options.flat ? '' : strings.dasherize(options.name) + '/') +\n      strings.dasherize(options.name) +","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/cdk/schematics/utils/build-component.ts#L44-L80","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (standalone app, no module)\nng generate component dashboard --module src/app/app.module.ts\n// after (standalone: don't pass --module; component registers itself)\nng generate component dashboard","handlingStrategy":"validation","validationCode":"import { Tree } from '@angular-devkit/schematics';\nfunction assertFileReadable(host: Tree, path: string): void {\n  if (host.read(path) === null) {\n    throw new Error(`File missing from tree: ${path}`);\n  }\n}\nassertFileReadable(host, 'src/app/app.module.ts');","typeGuard":"function hasFile(host: Tree, path: unknown): path is string {\n  return typeof path === 'string' && path.length > 0 && host.read(path) !== null;\n}","tryCatchPattern":"try {\n  const source = readIntoSourceFile(host, modulePath);\n} catch (e) {\n  if (e instanceof SchematicsException && e.message.startsWith('File ') && e.message.endsWith('does not exist.')) {\n    console.warn(`Skipping NgModule registration; module file missing: ${modulePath}`);\n    return;\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["angular","schematics","file-not-found","component-generation"],"backgroundTag":"module-file-not-found","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}