The right way to copy recordsdata and folders in Node.js? | by Sabesan Sathananthan


A number of methods to repeat recordsdata in Node.js

Photograph by Dziana Hasanbekava

In Node.js, there are a number of methods to repeat recordsdata., let’s check out the doable methods and evaluation every of them. That is my forty fourth Medium article.

The copyFile() perform, which may copy a file on to the vacation spot listing, performs the only motion.

fs.copyFile('./information.txt', './dest/information.txt');

The above technique, asynchronously copies the file from src to dest. If dest is already exists then by default it’s overwritten. There aren’t any args handed to the callback perform over than any doable exception. Node.js doesn’t make sure that copy operations are atomic. Node.js will try and delete the goal file if an error occurs after opening the goal file for writing.

There’s a drawback after we use the above technique. If the goal listing doesn’t exist then an exception will probably be thrown as a result of the goal listing should exist (the tactic is not going to robotically create the goal listing). Due to this fact, earlier than utilizing the above technique, consumer should validate whether or not the goal listing definetly exists or not? If the goal listing doesn’t exists, consumer may use fs.mkdir()or fs.mkdirSync()to create the goal listing. copyFile() technique can’t copy directories.

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here