angular/angular-cli · error · MissingFileReplacementException

File replacement (with) does not exist

Error message

File replacement (with) does not exist

What it means

normalizeFileReplacements validates each file replacement configured for the build before webpack runs. After resolving `replace` and `with` paths against the workspace root, it checks both files exist on disk; if the `with` (replacement source) file does not exist, MissingFileReplacementException is thrown with that path. The build aborts rather than silently substituting a non-existent file.

Source

Thrown at packages/angular_devkit/build_angular/src/utils/normalize-file-replacements.ts:38

  replace: string;
  with: string;
}

export function normalizeFileReplacements(
  fileReplacements: FileReplacement[],
  workspaceRoot: string,
): NormalizedFileReplacement[] {
  if (fileReplacements.length === 0) {
    return [];
  }

  const normalizedReplacement = fileReplacements.map((replacement) =>
    normalizeFileReplacement(replacement, workspaceRoot),
  );

  for (const { replace, with: replacementWith } of normalizedReplacement) {
    if (!existsSync(replacementWith)) {
      throw new MissingFileReplacementException(replacementWith);
    }

    if (!existsSync(replace)) {
      throw new MissingFileReplacementException(replace);
    }
  }

  return normalizedReplacement;
}

function normalizeFileReplacement(
  fileReplacement: FileReplacement,
  root: string,
): NormalizedFileReplacement {
  let replacePath: string;
  let withPath: string;
  if (fileReplacement.src && fileReplacement.replaceWith) {
    replacePath = fileReplacement.src;

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Verify the "with" path exists relative to the workspace root; correct or remove the fileReplacements entry in angular.json.
  2. If the file was renamed, update the replacement to the new name (e.g. src/environments/environment.development.ts).
  3. Run ls on the resolved path (workspaceRoot + "with" value) to confirm the exact location the CLI expects.

Example fix

// before (angular.json)
"fileReplacements": [{ "replace": "src/environments/environment.ts", "with": "src/environments/environment.prod.ts" }]
// after (file actually named environment.production.ts)
"fileReplacements": [{ "replace": "src/environments/environment.ts", "with": "src/environments/environment.production.ts" }]
Defensive patterns

Strategy: validation

Validate before calling

// Check before running the build
const { existsSync } = require('fs');
const { resolve } = require('path');
for (const fr of schema.fileReplacements ?? []) {
  if (!existsSync(resolve(workspaceRoot, fr.with))) {
    throw new Error(`fileReplacements 'with' target missing: ${fr.with}`);
  }
}

Try / catch

try {
  await runBuild(schema);
} catch (err) {
  if (err?.constructor?.name === 'MissingFileReplacementException' || /does not exist/.test(err?.message ?? '')) {
    console.error('Fix or remove the fileReplacements entry named in the message, then rebuild.');
    process.exitCode = 1;
  } else throw err;
}

Prevention

When it happens

Trigger: ng build with angular.json fileReplacements entry where the "with" (or legacy "replaceWith") path, resolved against workspaceRoot, does not exist on disk — e.g. src/environments/environment.prod.ts was deleted/renamed, or the path is misspelled or relative to the wrong root.

Common situations: Environment files removed in refactors but left in angular.json, branch switches where environment.*.ts files are gitignored or absent, monorepo configs where paths must be relative to workspaceRoot not the project dir, and configuration-driven replacements (dev/prod) that only exist for some targets.

Related errors


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