Sometimes, we want to copy a folder recursively in Node.js.
In this article, we’ll look at how to copy a folder recursively in Node.js.
How to copy a folder recursively in Node.js?
To copy a folder recursively in Node.js, we can use the copySync
method from the fs-extra
module.
For instance, we write
const fse = require('fs-extra');
const srcDir = `path/to/file`;
const destDir = `path/to/destination/directory`;
fse.copySync(srcDir, destDir, {
overwrite: true
}, (err) => {
if (err) {
console.error(err);
} else {
console.log("success!");
}
});
to call copySync
with the source srcDir
path, the destination destDir
path, and an object with overwrite
set to true
to overwrite any existing files with the same name, and a callback that runs when copySync
is done.
If there’re any errors, err
will be set.
Conclusion
To copy a folder recursively in Node.js, we can use the copySync
method from the fs-extra
module.