Fastest method to copy file in Node.js

There are ways to copy a file with just one line of code. One of such a method is

var fs = require('fs');

fs.createReadStream('test.log').pipe(fs.createWriteStream('newLog.log'));

In the later versions of Node.js copyFile was added so it’s even simpler. In Node.js 8.5.0 they introduced fs.copyFile and fs.copyFile Sync methods.

const fs = require('fs');

// destination.txt will be created or overwritten by default.

fs.copyFile('source.txt', 'destination.txt', (err) => {

  if (err) throw err;

  console.log('source.txt was copied to destination.txt');

});

Other methods include synchronous and asynchronous solutions and error handling also but it doesn’t check the existence of file as the above solution does. If it doesn’t check the existence of the file it is dangerous. Then the metadata with the files will also be lost.