Nodejs Copy Directory using npm script | copyfiles npm example

This tutorial covers how to copy files with the npm script tags command in package.json

  • copy files from one folder to another folder with npm scripts
  • npm script to copy directory including nested directory

You can also check other posts on npm command deprecate option is deprecated There are many npm packages to copy files, In this example, I am going to use copyFiles npm package.

Advantages with npm copy

As you are not worried about remembering the copy command in the OS environment, This will take care of running on Windows, Mac, and Unix flavors

How to copy files or directories in nodejs npm scripts

First, Go to your application project, Install copyfiles dependency with the below command Note: please add this as devDependencies using —save-dev

npm install --save-dev copyfiles

Or you can also copy the below code in package..json

    "devDependencies": {
        "copyfiles": "latest"


Next, run the npm install command to reinstall all dependencies. This will install the copyfiles library and copy it to the node_modules folder.

In the package.json file, There is a script tag, add the below line of code

"scripts": {
    "copy": "copyfiles source destination"

source: - input source directory or regular expression files target: - files copied to a target directory

Finally, You can script by calling the npm run copy command in terminal

npm copyfiles examples

Let’s discuss some examples

Copy specific files into directory:

Suppose you have a javascript file in the src/assets folder. How do you copy only javascript files to the output directory?

Below is the code with npm scripts,

"copyjavascript": "copyfiles src/assets/*.js destination"

Copy folder into destination directory:

If you want to copy the assets folder, assets contain js, CSS, and images folder, these files need to be copied to the dist/assets folder

Here is the npm script command

"copyFolder": "copyfiles src/assets dest"

Copy files with not overwriting the files:

By default, copyfiles copy and overwrite the files in the target folder,

There are an option -s or --soft soft copy to not override files if files exist

Here is a command

copy -s src dest

copy folders with nested path:

if you want to copy assets/sass/ to the sass-lib folder with the same nested folder structure

-u or --up with number is provided to maintain nested folder levels

copyfiles -u 2 "./assets/sass/\*\*" "./node_modules/sass-lib"

Nodejs copyfiles javascript code example

As of now, we have seen using copyfiles can be used as a command line or with npm scripts. You can use the server-side nodejs

First, you must import or import this library into your JavaScript codebase (ES6 versions)

var copyfiles = require("copyfiles");


copyfiles([filespaths], option, callbackfunction);

Files paths are the path of input and output. The path is a relative or absolute path for files or directories

Here is an example

const inputFolder = "./src/assets/";
const outputFolder = "./dist";
const copyfiles = require("copyfiles");

copyfiles([inputFolder, output], {}, (err) => {
  if (err) {
    console.log("Error occurred while copying", err);
  console.log("folder(s) copied to destination");


In Conclusion, Learned how to copy files from one directory to another in nodejs application