angular/angular-cli · error · MissingFileReplacementException

File replacement (replace) does not exist

Error message

File replacement (replace) does not exist

What it means

The counterpart check of normalizeFileReplacements: after the `with` file is verified, the `replace` (the file being replaced in the compilation, typically src/environments/environment.ts) must also exist on disk; otherwise MissingFileReplacementException is thrown for that path. Both sides of a replacement must be real files since webpack must read one and substitute the other.

Source

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

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;
    withPath = fileReplacement.replaceWith;
  } else if (fileReplacement.replace && fileReplacement.with) {
    replacePath = fileReplacement.replace;
    withPath = fileReplacement.with;

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Recreate or restore the file referenced by "replace" (e.g. src/environments/environment.ts) so it exists in the workspace.
  2. Update the "replace" path in angular.json to the actual base file location.
  3. Audit all fileReplacements entries across configurations with ls or a script before building.

Example fix

// before (angular.json)
"fileReplacements": [{ "replace": "src/config/environment.ts", "with": "src/config/environment.prod.ts" }]
// after (base file lives at src/environments/environment.ts)
"fileReplacements": [{ "replace": "src/environments/environment.ts", "with": "src/config/environment.prod.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.replace))) {
    throw new Error(`fileReplacements 'replace' base file missing: ${fr.replace}`);
  }
}

Try / catch

try {
  await runBuild(schema);
} catch (err) {
  if (err?.constructor?.name === 'MissingFileReplacementException' || /does not exist/.test(err?.message ?? '')) {
    console.error('The base file to replace is missing; restore it or update the fileReplacements path.');
    process.exitCode = 1;
  } else throw err;
}

Prevention

When it happens

Trigger: ng build with a fileReplacements entry whose "replace" path (resolved against workspaceRoot) does not exist — usually the base environment.ts was deleted/renamed, or the path points to the wrong project directory in a monorepo.

Common situations: Deleting the base environment file while keeping prod replacement entries, moving environments under a different folder, or multi-project repos where the replacement was copied between projects without adjusting the path.

Related errors


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