fs.copyFile()
Copies a file from a source path to a destination path.
Syntax
await copyFile(src, dest[, mode]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
src | string | Buffer | URL | Yes | The source filename to copy. |
dest | string | Buffer | URL | Yes | The destination filename. Overwritten if it exists. |
mode | integer | No | Copy modifiers, e.g. `constants.COPYFILE_EXCL` to fail if dest already exists. |
Returns
Promise<void> — Resolves once the copy completes.
Examples
import { copyFile } from 'node:fs/promises';
await copyFile('./config.json', './config.backup.json');
console.log('backed up');
Output
backed up
import { copyFile, constants } from 'node:fs/promises';
try {
await copyFile('a.txt', 'b.txt', constants.COPYFILE_EXCL);
} catch (err) {
console.log(err.code);
}
Output
EEXIST
Notes
Overwrites the destination by default. For copying directory trees
use `fs.cp(src, dest, { recursive: true })` (Node 16.7+). `copyFile`
copies a single regular file only.