How to copy folder recursively in Node.js ?

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 the 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 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 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 using the following command:

npm install ncp

Filename: recursiveCopy.js file

filter_none

edit
close

play_arrow

link
brightness_4
code

var 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');
});

chevron_right


Output:

Syntax:

fs.copy(source, destination, callback)

Parameter:

  • source: File path of the source folder.
  • destination: File path of destination folder.
  • callback: A function called at the completion of a given task and takes error as the first parameter. If an error occurs the callback function handles it without blocking the program execution. If 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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.