Dev Helpers
← Back

Detect an exclusive-create conflict

Detect an exclusive-create conflict using Node.js.

Open this example in the interactive library

Source code

import * as fs from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
const dir = await fs.mkdtemp(join(tmpdir(), "helpers-"));
const file = join(dir, "note.txt");
await fs.writeFile(file, "build");
try {
  console.log(
    await fs.writeFile(file, "again", { flag: "wx" }).then(
      () => false,
      (error) => error.code,
    ),
  );
} finally {
  await fs.rm(dir, { recursive: true, force: true });
}

Sample output

EEXIST