fs.copyFile() & fs.copyFileSync() in Node.js

Fs.copyFile();

fs.copyFile(src, dest[, flags], callback) is asynchronous copying of the file from source to destination. In case if destination already exists it will be overwritten. Callback can return nothing than possible exceptions. In case of errors while file has been opened for writing, Node.js will try to delete destination.

  • src is source file to copy and can be <string>, <URL>, <Buffer>.
  • dest is destination of the copying and can be <string>, <URL>, <Buffer>.
  • flags are modifiers for destination. 0 by default.
  • callback is a function.

Here is a code from Node.js open source side as an example for copying:

In case if we have a third argument and it’s a number we will have this code to perform copying:

Fs.copyFileSync();

 fs.copyFileSync(src, dest[, flags]) is synchronous copying of the file from source to destination. In case if destination already exists it will be overwritten. Returns undefined. In case of errors while file has been opened for writing, Node.js will try to delete destination.

  •  src is source file to copy and be <string>, <URL>, <Buffer>.
  • dest is destination of the copying and can be <string>, <URL>, <Buffer>.
  • flags are modifiers for destination. 0 by default.

And code for synchronous copying looks like example here:

As it was in case of asynchronous copying we have an option when the third parameter is specified as a number:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s