copyFile
Copy a file from src to dest. Does not copy directory trees.
Basic usage
import { copyFile } from '@rush-fs/core'
import { constants } from 'node:fs'
await copyFile('./a.txt', './b.txt')
await copyFile('./a.txt', './b.txt', constants.COPYFILE_EXCL) // fail if dest existsMethods
copyFile(src, dest, mode?)
Async. Returns Promise<void>.
| Argument | Type | Description |
|---|---|---|
src | string | Source file path. |
dest | string | Destination file path. |
mode | number | Optional. e.g. COPYFILE_EXCL to fail if dest exists. |
copyFileSync(src, dest, mode?)
Sync. Same arguments; throws on error.
Performance
From repo benchmarks (pnpm build && pnpm bench):
| Scenario | Node.js | Rush-FS | Speedup |
|---|---|---|---|
| copyFile 4 MB | 4.67 ms | 0.09 ms | 50x |
Rush-FS is much faster for single-file copy due to efficient native I/O.
Notes
- Directories: Use
cpwithrecursive: truefor directory trees.copyFileonly copies a single file. - COPYFILE_EXCL: Use
constants.COPYFILE_EXCLfromnode:fs(or the numeric value) to avoid overwriting an existing destination.
Last updated on