copy-dir vs copyfiles vs cpx vs fs-extra vs ncp
File and Directory Copying
copy-dircopyfilescpxfs-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
copy-dir0---6 years agoMIT
copyfiles0422-575 years agoMIT
cpx0527-3510 years agoMIT
fs-extra09,61757.7 kB132 months agoMIT
ncp0683-8011 years agoMIT

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

Recursive Copying

  • copy-dir:

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

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

  • 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

  • copy-dir:

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

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

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

  • copy-dir:

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

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

  • 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

  • copy-dir:

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

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

  • 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

  • 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!');
    });
    
  • 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!');
    });
    
  • 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: copy-dir vs copyfiles vs cpx vs fs-extra vs ncp

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

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

  • 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 copy-dir

copy-dir

Easy used 'copy-dir' lib, even use a filter, copy a file or directory to another path, when target path or parent target path not exists, it will create the directory automatically.

install

npm install copy-dir

grammar

Sync Mode:

copydir.sync(from, to[, options]);

Async Mode:

copydir(from, to, [options, ]callback);

[options]:

  utimes: false,  // Boolean | Object, keep addTime or modifyTime if true
  mode: false,    // Boolean | Number, keep file mode if true
  cover: true,    // Boolean, cover if file exists
  filter: true,   // Boolean | Function, file filter

filter is a function that you want to filter the path, then return true or false.

It can use three arguments named state, filepath, filename

  • state: String, 'file' / 'directory' / 'symbolicLink', marked as the file or path type
  • filepath: String, the file path
  • filename: String, the file name

usage

Sync Mode:

var copydir = require('copy-dir');

copydir.sync('/my/from/path', '/my/target/path', {
  utimes: true,  // keep add time and modify time
  mode: true,    // keep file mode
  cover: true    // cover file when exists, default is true
});

Async Mode:

var copydir = require('copy-dir');

copydir('/my/from/path', '/my/target/path', {
  utimes: true,  // keep add time and modify time
  mode: true,    // keep file mode
  cover: true    // cover file when exists, default is true
}, function(err){
  if(err) throw err;
  console.log('done');
});

add a filter

When you want to copy a directory, but some file or sub directory is not you want, you can do like this:

Sync Mode:

var path = require('path');
var copydir = require('copy-dir');

copydir.sync('/my/from/path', '/my/target/path', {
  filter: function(stat, filepath, filename){
    // do not want copy .html files
    if(stat === 'file' && path.extname(filepath) === '.html') {
      return false;
    }
    // do not want copy .svn directories
    if (stat === 'directory' && filename === '.svn') {
      return false;
    }
    // do not want copy symbolicLink directories
    if (stat === 'symbolicLink') {
      return false;
    }
    return true;  // remind to return a true value when file check passed.
  }
});
console.log('done');

Async Mode:

var path = require('path');
var copydir = require('copy-dir');

copydir('/a/b/c', '/a/b/e', {
  filter: function(stat, filepath, filename) {
    //...
    return true;
  }
}, function(err) {
  //...
});

Update Logs

1.3.0

Bug fix: filter function arguments incorrect, delete the third argument: dirname

Questions?

If you have any questions, please feel free to ask through New Issue.

License

copy-dir is available under the terms of the MIT License.