Filesystem
fs/promises.copyFile()
Copies file contents. Use COPYFILE_EXCL to avoid replacing an existing destination; copying is not an atomic publication operation. Tested on Node.js 22.16.0. Example output (one line per entry): ["catalog", "EEXIST"].
Formal Syntax & Structure
nodejs syntaxnodejs
copyFile(src, dest[, mode])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| src | string | URL | Source path. |
| dest | string | URL | Destination path. |
| mode | number | Copy flags. |
Return Value
Promise<void>
Completes after copying.
Working Example
Example Snippetnodejs
import * as fs from 'node:fs/promises';import { constants } from 'node:fs';import { join } from 'node:path';import { tmpdir } from 'node:os';const dir = await fs.mkdtemp(join(tmpdir(), 'bytemind-'));try { const p = join(dir, 'book.txt'); await fs.writeFile(p, 'catalog'); const q = join(dir, 'copy.txt'); await fs.copyFile(p, q, constants.COPYFILE_EXCL); console.log(await fs.readFile(q, 'utf8')); try { await fs.copyFile(p, q, constants.COPYFILE_EXCL); } catch (e) { console.log(e.code); }}finally { await fs.rm(dir, { recursive: true, force: true });}