Last updated: Apr 5, 2024
Reading timeยท6 min
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.
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); }
The example above assumes that you have the following folder structure.
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
.
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
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); }
If you are on Windows, your absolute paths may look similar to the following:
D:\\Bobby\\Desktop\\source-folder
D:\\Bobby\\Desktop\\destination-folder
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:
options
object.We set the recursive
property to true
in the options
object.
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.
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.
You can use the fs.cp()
method if you need to asynchronously copy a folder
recursively in Node.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', ); }, );
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:
options
object.error
parameter.If there is no error, then the error
parameter will be null
.
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.
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'); });
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.
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.
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.
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');
Make sure your environment supports top-level await to be able to run the code sample.
fs-extra
module to copy a folder recursively in Node.jsYou 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.
# ๐๏ธ 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.
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); }
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.
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.
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 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.
You can learn more about the related topics by checking out the following tutorials: