Copy a file
- Fast by using streams in the async version and
fs.copyFileSync()(when available) 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.
- 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
File you want to copy.
Type: string
Where you want the file copied.
Type: Object
Type: boolean
Default: true
Overwrite existing file.
Progress reporting. Only available when using the async method.
Type: Function
{
src: string,
dest: string,
size: number,
written: number,
percent: number
}srcanddestare absolute paths.sizeandwrittenare in bytes.percentis a value between0and1.
- For empty files, the
progressevent is emitted only once. - The
.on()method is available only right after the initialcpFile()call. So make sure you add ahandlerbefore.then():
(async () => {
await cpFile(source, destination).on('progress', data => {
// …
});
})();- 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
MIT © Sindre Sorhus