angular/angular-cli · error · Error

Failed to find chunk (${chunkId}) in set:\n${JSON.stringify(

Error message

Failed to find chunk (${chunkId}) in set:\n${JSON.stringify(chunks)}

What it means

markAsyncChunksNonInitial maps a list of async chunk IDs back to webpack Chunk objects. When an ID in asyncChunkIds has no matching chunk in the compilation's chunk set, it cannot determine initial/lazy status and throws, dumping the chunk set for diagnosis. This indicates the chunk ID list and the compilation chunks are out of sync.

Source

Thrown at packages/angular_devkit/build_angular/src/tools/webpack/utils/async-chunks.ts:38

  extraEntryPoints: NormalizedEntryPoint[],
): StatsChunk[] {
  const { chunks = [], entrypoints: entryPoints = {} } = webpackStats;

  // Find all Webpack chunk IDs not injected into the main bundle. We don't have
  // to worry about transitive dependencies because extra entry points cannot be
  // depended upon in Webpack, thus any extra entry point with `inject: false`,
  // **cannot** be loaded in main bundle.
  const asyncChunkIds = extraEntryPoints
    .filter((entryPoint) => !entryPoint.inject && entryPoints[entryPoint.bundleName])
    .flatMap((entryPoint) =>
      entryPoints[entryPoint.bundleName].chunks?.filter((n) => n !== 'runtime'),
    );

  // Find chunks for each ID.
  const asyncChunks = asyncChunkIds.map((chunkId) => {
    const chunk = chunks.find((chunk) => chunk.id === chunkId);
    if (!chunk) {
      throw new Error(`Failed to find chunk (${chunkId}) in set:\n${JSON.stringify(chunks)}`);
    }

    return chunk;
  });

  // A chunk is considered `initial` only if Webpack already belives it to be initial
  // and the application developer did not mark it async via an extra entry point.
  return chunks.map((chunk) => {
    return asyncChunks.find((asyncChunk) => asyncChunk === chunk)
      ? {
          ...chunk,
          initial: false,
        }
      : chunk;
  });
}

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Delete the .angular/cache directory and rebuild to clear stale chunk data.
  2. Remove stale node_modules/.cache and dist folders, then rebuild.
  3. Simplify/adjust `optimization.splitChunks` config that may be splitting async chunks unexpectedly.
  4. Update Angular CLI and webpack to compatible versions (known chunk-graph bugs were fixed upstream).

Example fix

// before
npx ng build --configuration production
// after
rm -rf .angular/cache dist && npx ng build --configuration production
Defensive patterns

Strategy: try-catch

Validate before calling

const missing = asyncChunkIds.filter(id => !chunks.some(c => c.id === id));
if (missing.length) throw new Error(`Chunks missing from compilation: ${missing.join(', ')}`);

Type guard

function allChunksPresent(ids: (string|number)[], chunks: { id?: string|number }[]): boolean {
  const known = new Set(chunks.map(c => c.id));
  return ids.every(id => known.has(id));
}

Try / catch

try {
  const stats = webpackStatsLogger(...);
} catch (err) {
  if (err instanceof Error && err.message.startsWith('Failed to find chunk')) {
    logger.warn('Chunk graph inconsistent; clearing webpack cache and retrying build.');
    await rebuildWithoutCache();
  } else throw err;
}

Prevention

When it happens

Trigger: Calling webpackStats (via markAsyncChunksNonInitial) when the stats/compilation contains async chunk IDs (e.g. from lazy-loaded modules or missing chunk merged states) that do not correspond to any chunk in the `chunks` collection — typically after chunk graph inconsistencies in webpack.

Common situations: Stale webpack cache after upgrading webpack or the CLI; corrupted .angular/cache; builds with aggressive chunk-splitting configs (splitChunks) that rename/remove chunks; webpack internal bugs with concatenated modules.

Related errors


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