Mark Krutik’s Open Source Blog

nodejs-frameworksfs.copyFile vs fs.copyFileSync

For this lab, I was supposed to create a wiki based on the research I had to do on a node.js fs module of my choice. I chose to do my research on the fs.copyFile module. The fs.copyFile consists of two modules one the is currently being mentioned and the fs.copyFileSync module. The fs.copyFile and fs.copyFileSync are used to copy a file from a source location to a destination specified by the module. The only difference is that one module is asynchronous while the other is synchronous. In the other parts of the blog, I will describe the two modules more in-depth and explain what is meant by the terms of asynchronous and synchronous.

fs.copyFile(src, dest[,flags], callback)

The fs.copyFile takes 4 parameters which are:

  1. src – Source is the file that you wish to copy. This parameter can be of type string, Buffer or URL.
  2. Destination – Is the place you want the file to be copied to. This parameter can be of type string, Buffer or URL.
  3. Flags –   Additional conditions that specify how a certain file will be copied. For example if you use the flag fs.constants.COPYFILE_EXCL, this means the copy operation will fail if a file destination already exists. This parameter is of type number.
  4. Callback – Is used in the parameter list when the process or function is asynchronous. This parameter is of type function.

Code Example

Code when flags are not used:

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');
});

Code when flags are used:

//If the third parameter is a number, than a flag is created
const fs = require('fs');
const { COPYFILE_EXCL } = fs.constants;
// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
fs.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL, callback);

fs.copyFileSync(src, dest[, flags])

The fs.copyFileSync takes 3 parameters which are:

  1. src – Source is the file that you wish to copy. This parameter can be of type string, Buffer or URL.
  2. Destination – Is the place you want the file to be copied to. This parameter can be of type string, Buffer or URL.
  3. Flags –   Additional conditions that specify how a certain file will be copied. For example if you use the flag fs.constants.COPYFILE_EXCL, this means the copy operation will fail if a file destination already exists. This parameter is of type number.

Code Example

Code when flags are not used:

const fs = require('fs');
// destination.txt will be created or overwritten by default.
fs.copyFileSync('source.txt', 'destination.txt');
console.log('source.txt was copied to destination.txt');

Code when flags are used:

// If the third parameter is a number, than a flag is created
const fs = require('fs');
const { COPYFILE_EXCL } = fs.constants;
// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
fs.copyFileSync('source.txt', 'destination.txt', COPYFILE_EXCL);

Conclusions

The difference between the modules is that the fs.copyFile fs module is an asynchronous operation while the fs.copyFileSync fs module is a synchronous operation. Asynchronous means one process at a time, while synchronous means occurring at the same time. If the module is asynchronous then it requires a callback parameter to be passed into the module, as opposed to the synchronous that has the same parameters as the fs.copyFile fs module, but no callback parameter to be passed into the module, which is why one module has 4 parameters, while the other module has 3 parameters. Since src and dest are self-explanatory, I will explain what flags represent. So, flags are used if you want to add an extra condition to your module. For example I already mentioned the fs.constants.COPYFILE_EXCL, which is a flag that checks if a destination.txt file already exists. If it exists the source file will not get copied into the destination file, unless the destination.txt file does not already exist.

References

For more references please refer to these websites for further information. These websites will give you more details on some other examples of flags and also the data types acceptable for the parameters of the modules mentioned. In addition, these references give more code examples.

  1. https://nodejs.org/api/fs.html#fs_fs_copyfile_src_dest_flags_callback
  2. https://nodejs.org/api/fs.html#fs_fs_copyfilesync_src_dest_flags
  3. https://github.com/nodejs/node/blob/cdb359840c9537e460b1bf0536fc36ec5fe2db2d/lib/fs.js#L1690
  4. https://github.com/nodejs/node/blob/cdb359840c9537e460b1bf0536fc36ec5fe2db2d/lib/fs.js#L1668

Thank you for reading this blog and I hope that this post helps anyone in understanding the concepts that were mentioned.

-Mark Krutik