Node.js Copy a Folder With Node fs-extra package

Introduction:

In this blog post, We learn how can you copy a directory or folder (which contains some files) to another location in Node.js. To do this here we are going to use a third-party library named as fs-extra.

fs-extra adds file system methods that aren’t included in the core fs module and adds promise support to the fs methods. It also uses graceful-fs to prevent EMFILE errors. It should be a drop in replacement for fs.

Installation of fs-extra package

To Install fs-extra package via NPM (Node Package Manager), run the following command in Terminal or command prompt:

npm install fs-extra

Let’s use fs-extra package to copy a directory in Node.js

// import fs-extra package
let fs = require("fs-extra");
 
 
// copy source folder to destination
fs.copy("source_folder", "destination_folder", function (err) {
    if (err){
        console.log('An error has beedn occured while copying the folder.')
        return 
    }
    console.log('Copying completed!')
});

Conclusion:

Here we have learnt how to use fs-extra module to copy folder or directory in Node.js.

References:

FS-EXTRA

#Happy Coding #TopCode

#copy folder node js #copy file to folder node js #copy folder node #copy directory in node js #node js copy folder contents #node js copy file to folder #copy directory node js #fs copy folder node js #copy folder in node js #copy folder recursively node js #node js copy folder sync #how to copy folder in nodejs #How to copy directory in node.js #how to copy non empty foler in node.js #how to copy folder with contents in node.js

, , , ,

Related posts

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site