Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    cp-filepublic

    cp-file Build Status Coverage Status

    Copy a file

    Highlights

    • 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.

    Install

    $ npm install cp-file
    

    Usage

    const cpFile = require('cp-file');
     
    (async () => {
        await cpFile('src/unicorn.png', 'dist/unicorn.png');
        console.log('File copied');
    })();

    API

    cpFile(source, destination, [options])

    Returns a Promise.

    cpFile.sync(source, destination, [options])

    source

    Type: string

    File you want to copy.

    destination

    Type: string

    Where you want the file copied.

    options

    Type: Object

    overwrite

    Type: boolean
    Default: true

    Overwrite existing file.

    cpFile.on('progress', handler)

    Progress reporting. Only available when using the async method.

    handler(data)

    Type: Function

    data
    {
        src: String,
        dest: String,
        size: Number,
        written: Number,
        percent: Number
    }
    • src and dest are absolute paths.
    • size and written are in bytes.
    • percent is a value between 0 and 1.
    Notes
    • For empty files, the progress event is emitted only once.
    • The .on() method is available only right after the initial cpFile() call. So make sure you add a handler before .then():
    (async () => {
        await cpFile(src, dest).on('progress', data => {
            // …
        });
    })();

    Related

    • 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

    License

    MIT © Sindre Sorhus

    install

    npm i cp-file

    Downloadsweekly downloads

    41,649

    version

    6.0.0

    license

    MIT

    repository

    githubgithub

    last publish

    collaborators

    • avatar