Open In App

How to copy folder recursively in Node.js ?

Last Updated : 04 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Node.js is an enhancement to the already existing JavaScript. It is used for server-side scripting. It is mostly used to develop dynamic web applications. Node.js developers prefer the use of asynchronous functions over synchronous functions as the former does not block program execution under any circumstances. Copying files or folders with multiple subfolders can be tedious using conventional Node.js file copying methods. However, the use of additional modules makes the task easier. 

The two modules in the discussion are the NCP module and the fs-extra module. Both modules support asynchronous functions to aid the process of copying the folders recursively. 

The examples below demonstrate the use of these modules for copying folders recursively.

Create a folder structure as follows 
in the working directory:
mkdir f1\f2\f3\f4\f5
notepad f1\f2\f3\f4\f5\new.txt
// Write some text and save the .txt file

Syntax:

ncp(source, destination, options, callback)

Parameter:

  • source: File path of the source folder.
  • destination: File path of the destination folder.
  • options
    • options.filter: A RegExp that can be used to specify if a folder/file must be copied or skipped.
    • options.transform: A function that can be used to apply streaming transforms while copying.
    • options.clobber: Can be set to true or false. If set to false, ncp does not overwrite files that already exist in the destination.
    • options.stopOnErr: Can be set to true or false. By default, it is set to false i.e. ncp continues to copy, log all errors, and returns an array. If set to true, ncp stops on the first error it encounters.
  • callback: A function called at the completion of a given task and takes an error as the first parameter. If an error occurs the callback function handles it without blocking the program execution.

Example 1: Using ncp module Install ncp module

npm install ncp

Filename: recursiveCopy.js file 

javascript




const ncp = require('ncp').ncp;
 
// Represents the number of pending
// file system requests at a time.
ncp.limit = 16;
 
// ncp(source, destination, callback)
ncp('D:\\HTML\\f1', 'D:\\HTML\\recursive',
        function (err) {
    if (err) {
        return console.error(err);
    }
    console.log('Folders copied recursively');
});


Output:

  

Syntax:

fs.copy(source, destination, callback)

Parameter:

  • source: File path of the source folder.
  • destination: File path of the destination folder.
  • callback: A function called at the completion of a given task and takes an error as the first parameter. If an error occurs the callback function handles it without blocking the program execution. If the callback is not passed, it returns promises.

Example 2: Using fs-extra module Install fs-extra module using the following command:

npm install fs-extra

Filename: fsrecur.js 

javascript




const fs = require('fs-extra');
 
// Async with promises:
fs.copy('D:\\HTML\\f1', 'D:\\HTML\\fsextra')
    .then(() => console.log('Files copied successfully!'))
    .catch(err => console.error(err));


Output:

  

All the folders within f1 starting from f2 up to f5 are copied recursively into the ‘recursive’ folder. Any file or folder within these folders gets copied to the new folder.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads