How to copy a Folder recursively in Node.js

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
6 min

banner

# Table of Contents

  1. How to copy a Folder recursively in Node.js
  2. Asynchronously copy a Folder recursively in Node.js
  3. Copy a Folder recursively in Node.js using Promises
  4. Using the fs-extra module to copy a folder recursively in Node.js

# How to copy a Folder recursively in Node.js

You can use the fs.cpSync() method to copy a folder recursively in Node.js.

The fs.cpSync() method synchronously copies the entire directory from the supplied src directory to the given destination directory.

The recursive property has to be set to true to copy the contents of the folder recursively.

index.js
import fs from 'fs'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() syntax // const fs = require('fs'); const sourceFolderPath = './source-folder'; const destinationFolderPath = './destination-folder'; try { fs.cpSync(sourceFolderPath, destinationFolderPath, { recursive: true, }); } catch (error) { console.log(error.message); }

source copied recursively to destination

The code for this article is available on GitHub

The example above assumes that you have the following folder structure.

shell
my-project/ โ””โ”€โ”€ source-folder/ โ””โ”€โ”€ a.txt โ””โ”€โ”€ nested-folder/ โ””โ”€โ”€ b.txt โ””โ”€โ”€ destination-folder/ โ””โ”€โ”€ index.js

This is what the destination-folder looks like after running node index.js.

copy folder recursively in node js

You can also pass absolute paths to the fs.cpSync method.

For example, if you are on macOS or Linux, your absolute paths will look like:

  • /path/to/source/folder
  • /path/to/destination/folder
index.js
import fs from 'fs'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() syntax // const fs = require('fs'); // ๐Ÿ‘‡๏ธ Using absolute paths const sourceFolderPath = '/path/to/source/folder'; const destinationFolderPath = '/path/to/destination/folder'; try { fs.cpSync(sourceFolderPath, destinationFolderPath, { recursive: true, }); console.log('Source copied recursively to destination'); } catch (error) { console.log(error.message); }
The code for this article is available on GitHub

If you are on Windows, your absolute paths may look similar to the following:

  • D:\\Bobby\\Desktop\\source-folder
  • D:\\Bobby\\Desktop\\destination-folder
index.js
import fs from 'fs'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() syntax // const fs = require('fs'); // const sourceFolderPath = './source-folder'; // const destinationFolderPath = './destination-folder'; const sourceFolderPath = 'D:\\Bobby\\Desktop\\source-folder'; const destinationFolderPath = 'D:\\Bobby\\Desktop\\destination-folder'; try { fs.cpSync(sourceFolderPath, destinationFolderPath, { recursive: true, }); console.log('Source copied recursively to destination'); } catch (error) { console.log(error.message); }

Notice that each backslash has to be escaped by another backslash to treat them as literal characters.

The fs.cpSync method synchronously copies the entire source directory to the specified destination directory, including subdirectories and files.

The arguments we passed to the fs.cpSync method are:

  1. The path to the source directory (can be absolute or relative).
  2. The path to the destination directory (can be absolute or relative).
  3. An options object.

We set the recursive property to true in the options object.

The recursive property defaults to false. When set to true, then the contents of the source directory are copied recursively.

There is also a force property in the options object.

index.js
fs.cpSync(sourceFolderPath, destinationFolderPath, { recursive: true, force: true, // ๐Ÿ‘ˆ๏ธ });

When the force property is set to true, then the method overwrites existing files or directories.

The force property is set to true by default.

# Asynchronously copy a Folder recursively in Node.js

You can use the fs.cp() method if you need to asynchronously copy a folder recursively in Node.js.

index.js
import fs from 'fs'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() syntax // const fs = require('fs'); const sourceFolderPath = './source-folder'; const destinationFolderPath = './destination-folder'; fs.cp( sourceFolderPath, destinationFolderPath, {recursive: true}, error => { if (error) { console.log(error.message); throw error; } console.log( 'The source folder copied successfully to the destination', ); }, );

asynchronously copy folder recursively in node js

The code for this article is available on GitHub

We passed relative paths to the fs.cp() method but you can also use absolute paths to the source and destination directories.

The fs.cp() method asynchronously copies the entire source directory structure to the given destination directory, including subdirectories and files.

The fs.cp() method takes the following arguments:

  1. A path to the source directory.
  2. A path to the destination directory.
  3. An options object.
  4. A callback function that takes an error parameter.

If there is no error, then the error parameter will be null.

# Copy a Folder recursively in Node.js using Promises

In more modern code, you might want to use Promises and async/await to copy the contents of a folder recursively.

In this case, you can use the fsPromises.cp() method.

index.js
import fsPromises from 'fs/promises'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() syntax // const fsPromises = require('fs/promises'); async function copyFolderRecursively( sourcePath, destinationPath, ) { try { await fsPromises.cp(sourcePath, destinationPath, { recursive: true, }); console.log( 'Source folder copied recursively to destination', ); } catch (err) { console.log(err.message); } } const sourceFolderPath = './source-folder'; const destinationFolderPath = './destination-folder'; copyFolderRecursively( sourceFolderPath, destinationFolderPath, ).then(() => { console.log('This runs after the Promise resolves'); });

copy folder recursively in node js using async await

The code for this article is available on GitHub

Notice that this time we import fsPromises from fs/promises.

The fs/promises module gives us access to promisified versions of the fs methods.

The copyFolderRecursively function takes 2 parameters - a source path and a destination path.

The method recursively copies the contents of the source directory to the given destination directory.

The fsPromises.cp() method returns a Promise that resolves with undefined upon success.

If an error is raised while copying the contents of the directory, the error gets passed to the catch() method.

The example above uses the .then() syntax when calling the function, however, in newer Node.js versions, you can also use the top-level await syntax.

index.js
import fsPromises from 'fs/promises'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() syntax // const fsPromises = require('fs/promises'); async function copyFolderRecursively( sourcePath, destinationPath, ) { try { await fsPromises.cp(sourcePath, destinationPath, { recursive: true, }); console.log( 'Source folder copied recursively to destination', ); } catch (err) { console.log(err.message); } } const sourceFolderPath = './source-folder'; const destinationFolderPath = './destination-folder'; // โœ… using top-level await await copyFolderRecursively( sourceFolderPath, destinationFolderPath, ); console.log('This runs after the Promise resolves');

You can also remove the async function to use top-level await in both places.

index.js
import fsPromises from 'fs/promises'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() syntax // const fsPromises = require('fs/promises'); const sourceFolderPath = './source-folder'; const destinationFolderPath = './destination-folder'; try { await fsPromises.cp(sourceFolderPath, destinationFolderPath, { recursive: true, }); console.log('Source folder copied recursively to destination'); } catch (err) { console.log(err.message); } console.log('This runs after the Promise resolves');
The code for this article is available on GitHub

Make sure your environment supports top-level await to be able to run the code sample.

# Using the fs-extra module to copy a folder recursively in Node.js

You can also use the popular fs-extra module to copy a folder recursively in Node.js.

Open your terminal in your project's root directory (where your package.json file is) to install the package.

shell
# ๐Ÿ‘‡๏ธ initialize package.json file npm init -y # ๐Ÿ‘‡๏ธ install using NPM npm install fs-extra # ๐Ÿ‘‡๏ธ or using YARN yarn add fs-extra

Now import and use the copySync method from the fs-extra package as follows.

index.js
import fs from 'fs-extra'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() syntax // const fs = require('fs-extra'); const sourceFolderPath = './source-folder'; const destinationFolderPath = './destination-folder'; try { fs.copySync(sourceFolderPath, destinationFolderPath); console.log('Source folder copied recursively to destination'); } catch (err) { console.log(err.message); }
The code for this article is available on GitHub

Notice that we import fs from fs-extra this time.

The copySync() method copies a file or a directory.

If a directory is copied, then it is copied recursively to the specified destination path.

You can also pass a filter function to the copySync() method.

index.js
import fs from 'fs-extra'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() syntax // const fs = require('fs-extra'); const sourceFolderPath = './source-folder'; const destinationFolderPath = './destination-folder'; const filterFunc = (src, dest) => { // Your logic here // It will be copied if this function returns true }; try { fs.copySync(sourceFolderPath, destinationFolderPath, { filter: filterFunc, }); console.log('Source folder copied recursively to destination'); } catch (err) { console.log(err.message); }

The filter function will be called with the source and destination directories.

The files and subfolders will be copied to the destination folder only if the filter function returns true.

The fs-extra package also has a copy method if you need to asynchronously copy a directory recursively.

index.js
import fs from 'fs-extra'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() syntax // const fs = require('fs-extra'); const sourceFolderPath = './source-folder'; const destinationFolderPath = './destination-folder'; async function copyDirectoryRecursively(src, dest) { try { await fs.copy(src, dest); console.log('Source folder copied to destination folder'); } catch (err) { console.log(err.message); } } copyDirectoryRecursively( sourceFolderPath, destinationFolderPath, ).then(() => { console.log('This runs after the Promise resolves'); });
The code for this article is available on GitHub

The copyDirectoryRecursively function is marked as async which enables us to use the async/await syntax.

The copy() method from the fs-extra package returns a Promise that resolves with undefined upon success.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev