How to copy folder recursively in Node.js ?
Last Updated :
04 Apr, 2023
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.
Similar Reads
How to Copy a File in Node.js? Node.js, with its robust file system (fs) module, offers several methods to copy files. Whether you're building a command-line tool, a web server, or a desktop application, understanding how to copy files is essential. This article will explore various ways to copy a file in Node.js, catering to bot
2 min read
How to Access the File System in Node.js ? In this article, we looked at how to access the file system in NodeJS and how to perform some useful operations on files. Prerequisite: Basic knowledge of ES6Basic knowledge of NodeJS NodeJS is one of the most popular server-side programming frameworks running on the JavaScript V8 engine, which uses
3 min read
How To Copy Files Recursively Using AWS S3 CP? S3 (Simple Storage Service) is a storage service provided by AWS to store any kind of data in File/Folder format. In this article, we will see how easily we can access S3 to store our data in buckets which are containers to store your data. The AWS Command Line Interface (AWS CLI) is an open-source
3 min read
How to read and write files in Node JS ? NodeJS provides built-in modules for reading and writing files, allowing developers to interact with the file system asynchronously. These modules, namely fs (File System), offer various methods for performing file operations such as reading from files, writing to files, and manipulating file metada
2 min read
Folder structure for a Node JS project Organizing a Node JS project with well planned folder structure is crucial for readability, scalability, and maintainability. A clear structure helps in managing code, configurations, modules, and other assets effectively. In this article, we are going to learn the folder structure of the Node JS pr
5 min read