How to copy files and folders in Node.js? | by Sabesan Sathananthan | Dec, 2022

Photo by Dziana Hasanbekava

In Node.js, there are multiple ways to copy files., let’s take a look at the possible ways and analysis each of them. This is my 44th Medium article.

The copyFile() function, which can copy a file directly to the destination directory, performs the simplest action.

fs.copyFile('./data.txt', './dest/info.txt');

The above method, asynchronously copies the file from src to dest. If dest is already exists then by default it is overwritten. There are no args passed to the callback function over than any possible exception. Node.js does not ensure that copy operations are atomic. Node.js will attempt to delete the target file if an error happens after opening the target file for writing.

There is a disadvantage when we use the above method. If the target directory does not exist then an exception will be thrown because the target directory must exist (the method will not automatically create the target directory). Therefore, before using the above method, user must validate whether the target directory definetly exists or not? If the target directory doesn’t exists, user could use fs.mkdir()or fs.mkdirSync()to create the target directory. copyFile() method can’t copy directories.

In this way, read the content of the source file and then write to the target file. If the content of the source file has to be modified during copying, this method is suitable

The disadvantage of this method is the same as the above copyFile() method. readFile() method is used to read the contents of the source file and writeFile() method can only write files in existing directories. By using this method we can’t copy directories. The content can be modified while being copied is the advantage of using this method.

readFile() method and writeFile() method are the whole block of operation data. If the file size is large the above method will put more strain on system resources. createReadStream() and createWriteStream() is to use the way of stream to manipulate data.

fs.createReadStream('./data.txt').pipe(fs.createWriteStream(`./info.txt`));

The new fs.cp() method has been added since version 16.7.0 of Node.js. By using this method, the entire directory structure including subdirectories and files can be copied asynchronously from src to dest. fs.cp() method can copy either a file or a directory. The configuration’s recursive property needs to be set to true if a directory copy is required.

To copy files

To copy the directory, including subdirectories and files.

As you can see, this fs.cp() method is much better than the above 3 methods.

  1. The dest directory doesn’t need to be required to exist. The dest directory will be automatically created if it doesn’t already exist (regardless of the level of a directory)
  2. You can completely copy files in the entire folder, including subdirectories, without recursively copying them separately.

When you are going to use this method first thing first you need to confirm the Node.js version!

What if you want to copy every file in the folder but you only have a lower version of Node.js? We can recursively copy some files in addition to the native cp command for Linux, which is covered in the next section:

How to use:

copyDir('./component', './page/home');

To execute Linux native commands, we may use the exec or spawn commands in child_process. To copy files or directories, the cp command in Linux is used.

You could able to use the above 5 methods if you are using the latest node version. Using the fs module in the node, I have shared the fastest ways to copy a file/directory. We utterly looked at the asynchronous methods that we accessed through the fs module of Node.js.

Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.

Looking to scale your software startup? Check out Circuit.


Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: