angular/components · error · SchematicsException
Could not find the project main file inside of the workspace
Error message
Could not find the project main file inside of the workspace config (${project.sourceRoot}) What it means
getProjectMainFile in src/cdk/schematics/utils/project-main-file.ts resolves the application's entry point by reading the "browser" (application builder) or "main" (browser builder) option from the project's build target options. If neither option is defined, the schematic cannot locate main.ts and throws this SchematicsException, including the project's sourceRoot for context.
Source
Thrown at src/cdk/schematics/utils/project-main-file.ts:23
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {Path} from '@angular-devkit/core';
import {SchematicsException} from '@angular-devkit/schematics';
import {getProjectTargetOptions} from './project-targets';
import {ProjectDefinition} from '@schematics/angular/utility';
/** Looks for the main TypeScript file in the given project and returns its path. */
export function getProjectMainFile(project: ProjectDefinition): Path {
const buildOptions = getProjectTargetOptions(project, 'build');
// `browser` is for the `@angular-devkit/build-angular:application` builder while
// `main` is for the `@angular-devkit/build-angular:browser` builder.
const mainPath = (buildOptions['browser'] || buildOptions['main']) as Path | undefined;
if (!mainPath) {
throw new SchematicsException(
`Could not find the project main file inside of the ` +
`workspace config (${project.sourceRoot})`,
);
}
return mainPath;
}
View on GitHub (pinned to 0411926e7d)
Solutions
- Run the schematic with --project pointing at an application, not a library; libraries have no main file.
- Add "options": { "main": "src/main.ts" } (or "browser" for the application builder) to the project's build target in angular.json.
- If you use a custom builder, expose the entry point via the standard main/browser option key so schematics can find it.
- Check which build target the schematic inspects and ensure that specific target (e.g. "build") defines the entry file.
Example fix
// before (angular.json)
"build": { "builder": "@angular-devkit/build-angular:browser", "options": {} }
// after (angular.json)
"build": { "builder": "@angular-devkit/build-angular:browser", "options": { "main": "src/main.ts" } } Defensive patterns
Strategy: validation
Validate before calling
const ws = JSON.parse(fs.readFileSync('angular.json', 'utf8'));
const p = ws.projects[name];
const opts = p?.architect?.build?.options ?? {};
if (!opts.main && !opts.browser) {
throw new Error(`Project "${name}" has no main/browser entry point; is it an application?`);
} Type guard
function hasMainFile(opts: Record<string, unknown> | undefined): opts is Record<string, unknown> & { main?: string; browser?: string } {
return !!opts && (typeof opts.main === 'string' || typeof opts.browser === 'string');
} Try / catch
try {
await generateSchematic('nav', { project: name });
} catch (e) {
if (String(e.message).includes('Could not find the project main file')) {
console.error(`"${name}" has no main file — run against an application project.`);
} else throw e;
} Prevention
- Only run app-oriented schematics against application projects, never libraries.
- Keep the standard main/browser option in the build target even with custom builders.
- Audit angular.json after migrations to ensure build options survived.
When it happens
Trigger: Running CDK/Material schematics against a project whose build target (build or the target resolved by getProjectTargetOptions) has no "main" or "browser" option — e.g. library projects, custom builders (ngx-build-plus, custom executors), or hand-trimmed angular.json entries.
Common situations: Running `ng add @angular/material` or `ng generate @angular/cdk:xxx` targeting a library instead of an application; migrating from older builder configs where main was moved or renamed; Nx/custom executor setups that keep the entry point in a different key.
Related errors
- Could not find project in workspace: ${projectName}
- Cannot determine project target configuration for: ${buildTa
- Cannot read "${filePath}" because it does not exist.
- No data could be found for target version: ${version}
- Could not find file for path: ${path}
AI-assisted analysis of angular/components@0411926e7d (2026-08-31).
Data as JSON: /api/errors/a466e058d94aa009.
Report an issue: GitHub.