Copy a file
- Fast by using streams in the async version and
fs.copyFileSync()
in the synchronous version. - 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.
$ npm install cp-file
const cpFile = require('cp-file');
(async () => {
await cpFile('source/unicorn.png', 'destination/unicorn.png');
console.log('File copied');
})();
Returns a Promise
that resolves when the file is copied.
Type: string
The file you want to copy.
Type: string
Where you want the file copied.
Type: object
Type: boolean
Default: true
Overwrite existing destination file.
Progress reporting. Only available when using the async method.
Type: Function
{
sourcePath: string,
destinationPath: string,
size: number,
writtenBytes: number,
percent: number
}
source
anddestination
are absolute paths.size
andwrittenBytes
are in bytes.percent
is a value between0
and1
.
- For empty files, the
progress
event is emitted only once. - The
.on()
method is available only right after the initialcpFile()
call. So make sure you add ahandler
before.then()
:
const cpFile = require('cp-file');
(async () => {
await cpFile(source, destination).on('progress', data => {
// …
});
})();