angular/angular-cli · error · Error

Cannot find 'main' entrypoint.

Error message

Cannot find 'main' entrypoint.

What it means

find-tests-plugin is a webpack plugin for the karma builder that augments the 'main' entrypoint with discovered spec files. It expects the webpack configuration's entry factory to return an entrypoints object containing a 'main' key with an `import` array. If the 'main' entrypoint is missing or lacks an `import` property, it throws, because it cannot inject test files.

Source

Thrown at packages/angular_devkit/build_angular/src/builders/karma/find-tests-plugin.ts:50

    const {
      include = ['**/*.spec.ts'],
      exclude = [],
      projectSourceRoot,
      workspaceRoot,
    } = this.options;
    const webpackOptions = compiler.options;
    const entry =
      typeof webpackOptions.entry === 'function' ? webpackOptions.entry() : webpackOptions.entry;

    let originalImport: string[] | undefined;

    // Add tests files are part of the entry-point.
    webpackOptions.entry = async () => {
      const specFiles = await findTests(include, exclude, workspaceRoot, projectSourceRoot);
      const entrypoints = await entry;
      const entrypoint = entrypoints['main'];
      if (!entrypoint.import) {
        throw new Error(`Cannot find 'main' entrypoint.`);
      }

      if (specFiles.length) {
        originalImport ??= entrypoint.import;
        entrypoint.import = [...originalImport, ...specFiles];
      } else {
        assert(this.compilation, 'Compilation cannot be undefined.');
        this.compilation
          .getLogger(pluginName)
          .error(`Specified patterns: "${include.join(', ')}" did not match any spec files.`);
      }

      return entrypoints;
    };

    compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
      this.compilation = compilation;
      compilation.contextDependencies.add(projectSourceRoot);

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Ensure the karma builder options provide a valid `main` entrypoint file (e.g. src/test.ts) or rely on a builder version that supplies defaults.
  2. Check for other plugins mutating `entry` and remove/reorder them so find-tests-plugin sees an esbuild-style entry object with `import`.
  3. Verify you are not overriding entry to a string/array form in a custom webpack merge; keep the object-of-entrypoints shape.
  4. Update @angular-devkit/build-angular to a version where the karma builder always produces the expected entry structure.

Example fix

// before (builder options)
{ "builder": "@angular-devkit/build-angular:karma", "options": {} }
// after
{ "builder": "@angular-devkit/build-angular:karma", "options": { "main": "src/test.ts" } }
Defensive patterns

Strategy: validation

Validate before calling

const entrypoints = await entry();
if (!entrypoints['main']?.import) {
  throw new Error('karma entry must contain a main entrypoint with an import array');
}

Type guard

function hasMainImport(e: unknown): e is { main: { import: string[] } } {
  return !!e && typeof e === 'object' && 'main' in e &&
    !!((e as any).main) && Array.isArray((e as any).main.import);
}

Try / catch

try {
  applyFindTestsPlugin(compiler, options);
} catch (e) {
  if (e.message.includes("Cannot find 'main' entrypoint")) {
    console.error('Set the karma builder "main" option (e.g. src/test.ts).');
  } else throw e;
}

Prevention

When it happens

Trigger: The karma builder's entry factory returns entrypoints without a 'main' key (e.g. options.main was not set, or entry is a different shape); the entrypoint object lacks an `import` array (incompatible webpack entry format or another plugin rewrote it).

Common situations: Configuring the karma builder without a `main` option and without built-in defaults; another webpack plugin replacing entry with a single string/object form; upgrading the karma builder against an older webpack entry configuration.

Related errors


AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30). Data as JSON: /api/errors/8da0025e57618245. Report an issue: GitHub.