Copy a file
npm install copy-file> Copy a file
- It's super fast by cloning the file whenever possible.
- Resilient by using graceful-fs.
- User-friendly by creating non-existent destination directories for you.
- Can be safe by turning off overwriting.
- Preserves file mode but not ownership.
- User-friendly errors.
``sh`
npm install copy-file
`js
import {copyFile} from 'copy-file';
await copyFile('source/unicorn.png', 'destination/unicorn.png');
console.log('File copied');
`
Returns a Promise that resolves when the file is copied.
The file is cloned if the onProgress option is not passed and the file system supports it.
#### source
Type: string
The file you want to copy.
The file is cloned if the file system supports it.
#### destination
Type: string
Where you want the file copied.
#### options
Type: object
##### overwrite
Type: boolean\true
Default:
Overwrite existing destination file.
##### cwd
Type: string\process.cwd()
Default:
The working directory to find source files.
The source and destination path are relative to this.
##### directoryMode
Type: number\0o777
Default:
Permissions for created directories.
It has no effect on Windows.
##### onProgress
Type: (progress: ProgressData) => void
The given function is called whenever there is measurable progress.
Only available when using the async method.
###### ProgressData
`js`
{
sourcePath: string,
destinationPath: string,
size: number,
writtenBytes: number,
percent: number
}
- sourcePath and destinationPath are absolute paths.size
- and writtenBytes are in bytes.percent
- is a value between 0 and 1.
###### Notes
- For empty files, the onProgress callback function is emitted only once.
`js
import {copyFile} from 'copy-file';
await copyFile(source, destination, {
onProgress: progress => {
// …
}
});
``
- cpy - Copy files
- cpy-cli - Copy files on the command-line
- move-file - Move a file
- make-dir - Make a directory and its parents if needed