{

Nodejs Copy Directory using npm script | copyfiles npm example


How to copy directory with npm script  copyfiles npm

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

  • copy files from one folder to another 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 worried about remembering the copy command in OS environment, This will take care of running on Windows, Mac, Unix flavors

How to copy files or directory 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 below code in package..json

    "devDependencies": {
        "copyfiles": "latest"
       
    },

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

In the 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 a 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 file in 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,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 overwrite 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 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');

Syntax:

copyfiles([filespaths], option, callbackfunction);

Filespaths are the path and 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");
});

Conclusion

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

THE BEST NEWSLETTER ANYWHERE
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
Subscribe
You'll get a notification every time a post gets published here.