How to copy directory with npm script copyfiles npm

This tutorials covers how to copy files with script tags command in package.json

  • copy files from one folder to other folder
  • npm script to copy directory including nested directory

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 worry about remembering the copy command in OS environment, This will take care of running on Windows, Mac, Unix flavours

How to copy files or directory in nodejs npm scripts

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

npm install --save-dev copyfiles

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

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

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

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

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

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

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

npm copyfiles examples

Let’s discuss some examples

Copy specific files into directory

Suppose you have a javascript files in src/assets folder. How do you copy only javascript files to 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 assets folder, assets contains js,css,images folder, these files need to be copied to dist/assets folder

Here is npm script command

"copyFolder": "copyfiles src/assets dest"

Copy files with not overrite the files

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

There is an option -s or --soft soft copy to not override files if files exists

Here is an command

copy -s src dest

copy folders with nested path

if you want to copy assets/sass/ to sass-lib folder with 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 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);

Filespaths is the path and path of input and output. The path is 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");
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.