copyfiles vs cpx vs copy-dir vs fs-extra vs ncp
File and Directory Copying
copyfilescpxcopy-dirfs-extrancpSimilar Packages:

File and Directory Copying

File and Directory Copying libraries in Node.js provide developers with tools to copy files and directories programmatically. These libraries offer various features such as recursive copying, preserving file attributes, handling symbolic links, and providing progress callbacks. They are useful for tasks like building automation, file management, and creating deployment scripts. Each library has its own set of features, performance characteristics, and API designs, catering to different use cases and preferences.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
copyfiles1,752,985422-575 years agoMIT
cpx368,928529-3510 years agoMIT
copy-dir103,415---6 years agoMIT
fs-extra09,62457.7 kB1217 days agoMIT
ncp0682-8011 years agoMIT

Feature Comparison: copyfiles vs cpx vs copy-dir vs fs-extra vs ncp

Recursive Copying

  • copyfiles:

    copyfiles supports recursive copying of files and directories using glob patterns, allowing for selective copying based on file matching.

  • cpx:

    cpx supports recursive copying of files and directories, with the added feature of watching for changes and copying incrementally.

  • copy-dir:

    copy-dir provides simple recursive copying of directories, making it easy to copy all contents without additional configuration.

  • fs-extra:

    fs-extra offers robust recursive copying capabilities, including the ability to copy entire directories while preserving file attributes and structure.

  • ncp:

    ncp specializes in recursive copying, preserving file attributes, and handling symbolic links, making it efficient for comprehensive directory copying.

File Attribute Preservation

  • copyfiles:

    copyfiles does not focus on preserving file attributes; its primary feature is copying files based on glob patterns.

  • cpx:

    cpx preserves file attributes during copying, but its main focus is on watching files and copying them when changes occur.

  • copy-dir:

    copy-dir preserves basic file attributes during copying, but it does not explicitly handle symbolic links or advanced attributes.

  • fs-extra:

    fs-extra excels at preserving file attributes, including timestamps, permissions, and symbolic links, providing a more complete file copying solution.

  • ncp:

    ncp preserves file attributes, including timestamps and symbolic links, ensuring that the copied files maintain their original characteristics.

Command-Line Interface (CLI)

  • copyfiles:

    copyfiles offers a powerful CLI for copying files and directories, making it easy to integrate into build scripts and automation tasks.

  • cpx:

    cpx provides a simple CLI for copying files and directories, with options for watching and incremental copying, making it user-friendly for developers.

  • copy-dir:

    copy-dir does not provide a built-in CLI; it is primarily a programmatic library for use in Node.js applications.

  • fs-extra:

    fs-extra does not have a dedicated CLI, but it extends the fs module, allowing its methods to be used in any Node.js script.

  • ncp:

    ncp does not provide a CLI; it is designed to be used as a programmatic library within Node.js applications.

Watching for Changes

  • copyfiles:

    copyfiles does not include change-watching features; it focuses on copying files based on specified patterns.

  • cpx:

    cpx includes built-in support for watching files and directories, automatically copying them when changes are detected, which is useful for development workflows.

  • copy-dir:

    copy-dir does not support watching for changes; it is a straightforward copying library without real-time capabilities.

  • fs-extra:

    fs-extra does not have built-in change-watching capabilities; it is focused on enhancing file system operations in Node.js.

  • ncp:

    ncp does not support watching for changes; it is designed for one-time recursive copying of files and directories.

Ease of Use: Code Examples

  • copyfiles:

    File Copying with Glob Patterns using copyfiles

    const copyfiles = require('copyfiles');
    
    copyfiles(['src/*.js', 'src/styles/*.css'], 'dist', (err) => {
      if (err) throw err;
      console.log('Files copied successfully!');
    });
    
  • cpx:

    Incremental Copying with Change Watching using cpx

    const cpx = require('cpx');
    
    cpx.copy('src/**/*', 'dest', { watch: true }, () => {
      console.log('Files copied and watching for changes!');
    });
    
  • copy-dir:

    Simple Directory Copying with copy-dir

    const copyDir = require('copy-dir');
    
    copyDir('sourceDir', 'destDir', (err) => {
      if (err) throw err;
      console.log('Directory copied successfully!');
    });
    
  • fs-extra:

    Comprehensive File Operations with fs-extra

    const fs = require('fs-extra');
    
    fs.copy('sourceDir', 'destDir', (err) => {
      if (err) throw err;
      console.log('Directory copied with attributes!');
    });
    
  • ncp:

    Simple Recursive Copying with ncp

    const ncp = require('ncp').ncp;
    
    ncp('sourceDir', 'destDir', (err) => {
      if (err) return console.error(err);
      console.log('Directory copied successfully!');
    });
    

How to Choose: copyfiles vs cpx vs copy-dir vs fs-extra vs ncp

  • copyfiles:

    Select copyfiles if you require a command-line tool for copying files and directories with support for glob patterns, including the ability to create target directories automatically. It is great for build scripts and automation tasks.

  • cpx:

    Opt for cpx if you want a tool that supports one-way file and directory copying with built-in support for watching files and incremental copying. It is useful for development workflows where you need to keep directories in sync.

  • copy-dir:

    Choose copy-dir if you need a simple and lightweight solution for recursively copying directories with minimal configuration. It is ideal for straightforward copying tasks without additional dependencies.

  • fs-extra:

    Choose fs-extra if you need a comprehensive file system library that extends the built-in fs module with additional features like recursive directory copying, file moving, and more. It is suitable for projects that require robust file system operations beyond just copying.

  • ncp:

    Select ncp if you need a simple and efficient solution for recursively copying files and directories, especially if you want to preserve file attributes and handle symbolic links. It is lightweight and easy to use for quick copying tasks.

README for copyfiles

copyfiles Build Status

copy files easily

Install

npm install copyfiles -g

Command Line

  Usage: copyfiles [options] inFile [more files ...] outDirectory

  Options:
    -u, --up       slice a path off the bottom of the paths               [number]
    -a, --all      include files & directories begining with a dot (.)   [boolean]
    -f, --flat     flatten the output                                    [boolean]
    -e, --exclude  pattern or glob to exclude (may be passed multiple times)
    -E, --error    throw error if nothing is copied                      [boolean]
    -V, --verbose  print more information to console                     [boolean]
    -s, --soft     do not overwrite destination files if they exist      [boolean]
    -F, --follow   follow symbolink links                                [boolean]
    -v, --version  Show version number                                   [boolean]
    -h, --help     Show help                                             [boolean]

copy some files, give it a bunch of arguments, (which can include globs), the last one is the out directory (which it will create if necessary). Note: on windows globs must be double quoted, everybody else can quote however they please.

copyfiles foo foobar foo/bar/*.js out

you now have a directory called out, with the files foo and foobar in it, it also has a directory named foo with a directory named bar in it that has all the files from foo/bar that match the glob.

If all the files are in a folder that you don't want in the path out path, ex:

copyfiles something/*.js out

which would put all the js files in out/something, you can use the --up (or -u) option

copyfiles -u 1 something/*.js out

which would put all the js files in out

you can also just do -f which will flatten all the output into one directory, so with files ./foo/a.txt and ./foo/bar/b.txt

copyfiles -f ./foo/*.txt ./foo/bar/*.txt out

will put a.txt and b.txt into out

if your terminal doesn't support globstars then you can quote them

copyfiles -f ./foo/**/*.txt out

does not work by default on a mac

but

copyfiles -f "./foo/**/*.txt" out

does.

You could quote globstars as a part of input:

copyfiles some.json "./some_folder/*.json" ./dist/ && echo 'JSON files copied.'

You can use the -e option to exclude some files from the pattern, so to exclude all all files ending in .test.js you could do

copyfiles -e "**/*.test.js" -f ./foo/**/*.js out

Other options include

  • -a or --all which includes files that start with a dot.
  • -s or --soft to soft copy, which will not overwrite existing files.
  • -F or --follow which follows symbolinks

copyup

also creates a copyup command which is identical to copyfiles but -up defaults to 1

Programic API

var copyfiles = require('copyfiles');

copyfiles([paths], opt, callback);

takes an array of paths, last one is the destination path, also takes an optional argument which the -u option if a number, otherwise if it's true it's the flat option or if it is an object it is a hash of the various options (the long version e.g. up, all, flat, exclude, error, verbose, follow, and soft)

Tilde support for home directory

when the src/dest path start with tilde for home directory under windows, please make sure -u or -f is added in options or use copyup command. if not you will get Error: Illegal characters in path.