The Wayback Machine - https://web.archive.org/web/20240916125001/https://www.geeksforgeeks.org/node-js-fspromises-copyfile-method/
Open In App

Node.js fsPromises.copyFile() Method

Last Updated : 08 Oct, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The fsPromises.copyFile() method is used to asynchronously copy a file from the source path to destination path. By default, destination path is overwritten if it already exists. The Promise will be resolved with no arguments upon success.

Syntax:

fsPromises.copyFile( src, dest, flags )

Parameters: This method accepts three parameters as mentioned above and described below:

  • src: It is a String, Buffer or URL that denotes the source filename to copy.
  • dest: It is a String, Buffer or URL that denotes the destination filename that the copy operation would create.
  • flags: It is a number modifier for copy operation. Default value is 0. flags is an optional integer that specifies the behavior of the copy operation. It is possible to create a mask consisting of the bitwise OR of two or more values.
    1. fs.constants.COPYFILE_EXCL: The copy operation will fail if dest already exists.
    2. fs.constants.COPYFILE_FICLONE: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used.
    3. fs.constants.COPYFILE_FICLONE_FORCE: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail.

Return Value: Promise. The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.

Create an original.txt file in the given directory to perform the following method. This example shows the copy operation of the original.txt file to copied.txtfile, if flag  is not given.

Filename: index.js




// Node.js program to demonstrate the 
// fsPromises.copyFile() method 
     
// Import the filesystem module 
const fs = require('fs'); 
const fsPromises = require('fs').promises;
  
fsPromises.copyFile("original.txt", "copied.txt")
.then(function() {
  console.log("File Copied");
})
.catch(function(error) {
  console.log(error);
});


Step to run this program:
Run index.js file using the following command:

node index.js

Output:

File Copied

Now you can see the copied.txt file is created in your current root directory.

Reference: https://nodejs.org/api/fs.html#fs_fspromises_copyfile_src_dest_mode


Previous Article
Next Article

Similar Reads

Node fs.copyFile() Function
The fs.copyFile() method is used to asynchronously copy a file from the source path to the destination path. By default, Node.js will overwrite the file if it already exists at the given destination. The optional mode parameter can be used to modify the behavior of the copy operation. Syntax: fs.copyFile( src, dest, mode, callback )Parameters: This
3 min read
ASP CopyFile Method
The ASP CopyFile Method is used to copying one or more files from one place to another place. It is a predefined method of the FileSystem Object. Syntax: FileSystemObject.CopyFile source,destination[,overwrite] Parameter Values: Source: It is a required attribute. It contains a Character string file that can include wildcard characters, for one or
1 min read
Node.js fsPromises.truncate() Method
The fsPromises.truncate() method in node.js is used to change the size of the file i.e. either increase or decrease the file size. This method changes the length of the file at the path by len bytes. If len represents a length shorter than the file’s current length, the file is truncated to that length. If it is greater than the file length is padd
2 min read
Node.js fsPromises.chown() Method
The fsPromises.chown() method is used to change the ownership of a file then resolves the Promise with no arguments upon success. The function accepts a user id and group id that can be used to set the respective owner and group. Syntax: fsPromises.chown( path, uid, gid) Parameters: This method accepts threeparameters as mentioned above and describ
2 min read
Node.js fsPromises.mkdir() Method
The fsPromises.mkdir() method is used to asynchronously create a directory then resolves the Promise with either no arguments, or the first directory path created if recursive is true. Syntax: fsPromises.mkdir(path, options) Parameters: This method accept two parameters as mentioned above and described below: path: This parameter is a String, Buffe
1 min read
Node.js fsPromises.mkdtemp() Method
The fsPromises.mkdtemp() method is an inbuilt method which creates a unique temporary directory and resolves the Promise with the created directory path. Syntax: fs.Promises.mkdtemp( prefix, options ) Parameters: The method accepts two parameters as mentioned above and described below: prefix: It is a string that denotes the path of the file. optio
2 min read
Node.js fsPromises.realpath() Method
The fsPromises.realPath() method determines the actual location of path using the same semantics as the fs.realpath.native() function then resolves the Promise with the resolved path. Only paths that can be converted to UTF8 strings are supported. Syntax: fsPromises.realpath( path, options ) Parameters: This method accepts two parameters as mention
2 min read
Node.js fsPromises.access() Method
The fsPromises.access() method is used to test the permissions of a given file or directory specified by path. The permissions to be checked can be specified as a parameter using file access constants. It is also possible to check multiple file permissions by using the bitwise OR operator to create a mask with more than one file constant. If the ac
3 min read
Node.js fsPromises.lstat() Method
The fs.promises.lstat() method is defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the user's computers. The lstat() method gives some information specific to files and folders using methods defines on stats objects (data provided by lstat). The fs.promises.lstat() method returns a
3 min read
Node.js fsPromises.open() Method
The fsPromises.open() method is used to asynchronously open a file that returns a Promise that, when resolved, yields a FileHandle object. Syntax: fsPromises.open( filename, flags, mode) Parameter: This method accepts three parameters as mentioned above and described below: filename: It is a String, Buffer, or URL that holds the name of the file to
2 min read
Node.js fsPromises.stat() Method
The fsPromises.stat() method is used to return information about the given file or directory. The Promise is resolved with the fs.Stats object for the given path. Syntax: fsPromises.stat( path, options ) Parameters: This method accept two parameters as mentioned above and described below: path: It holds the path of the file or directory that has to
2 min read
Node.js fsPromises.chmod() Method
The fsPromises.chmod() method is used to change the permissions of a given path. These permissions can be specified using string constants or octal numbers that correspond to their respective file modes. Note: The Windows platform only supports the changing of the write permission. It does not support the distinction between the permissions of the
3 min read
Node.js fsPromises.readFile() Method
The fsPromises.readFile() method is used to read the file. This method read the entire file into the buffer. To load the fs module, we use require() method. It Asynchronously reads the entire contents of a file. Syntax: fsPromises.readFile( path, options ) Parameters: The method accepts two parameters as mentioned above and described below: path: I
2 min read
Node.js fsPromises.writeFile() Method
The fsPromises.writeFile() method is used to asynchronously write the specified data to a file. By default, the file would be replaced if it exists. The ‘options’ parameter can be used to modify the functionality of the method. The Promise will be resolved with no arguments upon success. Syntax: fsPromises.writeFile( file, data, options ) Parameter
2 min read
Node.js fsPromises.utimes() Method
The fsPromises.utimes() method is used to asynchronously change the modification and access timestamps of a file. The timestamps can be specified using a number, string, or Date object. An error would be thrown if the timestamp cannot be converted to a proper number, or is NaN, Infinity or -Infinity. It changes the file system timestamps of the obj
2 min read
Node.js fsPromises.opendir() Method
The fsPromises.opendir() method is used to asynchronously open a directory in the file system. It creates an fs.Dir, which contains all further functions for reading from and cleaning up the directory. This object contains various methods that can be used to accessing the directory. Syntax: fsPromises.opendir( path[, options]) Parameters: This meth
2 min read
Node.js fsPromises.symlink() Method
The fsPromises.symlink() method is used to create a symlink to the specified path then resolves the Promise with no arguments upon success. This creates a link making the path point to the target. The relative targets are relative to the link’s parent directory. Syntax: fsPromises.symlink( target, path, type ) Parameters: This method accept three p
2 min read
Node.js fsPromises.lchown() Method
The fsPromises.lchown() method is used to change the ownership of a file then resolves the Promise with no arguments upon success. The function accepts a user id and group id that can be used to set the respective owner and group. Syntax: fsPromises.lchown( path, uid, gid ) Parameters: This method accepts three parameters as mentioned above and des
2 min read
Node.js fsPromises.lchmod() Method
The fsPromises.lchmod() method is used to change the permissions of a given path. These permissions can be specified using string constants or octal numbers that correspond to their respective file modes. Note: This method is only implemented on macOS. It changes the permissions of a file then resolves the Promise with no arguments upon success. Sy
2 min read
Node.js fsPromises.rmdir() Method
The fsPromises.rmdir() method is used to delete a directory at the given path. It can also be used recursively to remove nested directories. It resolves the Promise with no arguments upon success. Syntax: fsPromises.rmdir( path, options ) Parameters: This method accept two parameters as mentioned above and described below: path: It holds the path o
3 min read
Node.js fsPromises.rename() Method
The fsPromises.rename() method is used to asynchronously rename a file at the given old path to a given new path. It will overwrite the destination file if it already exists. It resolves the Promise with no arguments upon success. Syntax: fsPromises.rename( oldPath, newPath ) Parameters: This method accept two parameters as mentioned above and desc
2 min read
Node.js fsPromises.truncate() Method
The fsPromises.truncate() method is defined in the File System module of Node.js. The File System module is basically used to interact with the hard-disk of the user's computer. The truncate() method is used to modify the inner contents of the file by 'len' bytes. If len is shorter than the file’s current length, the file is truncated to that lengt
3 min read
Node.js fsPromises.opendir() Method
The fsPromises.opendir() method is defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the user's computer. The method is used to asynchronously open a directory.The fsPromise.opendir() method returns a resolved or rejected promise and hence avoid the callback nesting or callback hell
4 min read
Node.js fsPromises.appendFile() Function
The fsPromises.appendFile() method is used to asynchronously append the given data to a file. A new file is created if it does not exist. The options parameter can be used to modify the behavior of the operation. Syntax: fsPromises.appendFile( path, data, options ) Parameters: This method accepts three parameters as mentioned above and described be
2 min read
Node.js Http2ServerRequest.method Method
The Http2ServerRequest.method is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to get the string representation of the request method. Syntax: const request.method Parameters: This method does not accept any argument as a parameter. Return Value: This method returns the string represe
3 min read
Node.js http.IncomingMessage.method Method
The http.IncomingMessage.method is an inbuilt application programming interface of class Incoming Message within the inbuilt http module which is used to get the type of request method as a string. Syntax: request.method Parameters: This method does not accept any argument as a parameter. Return Value: This method returns the request type name as a
2 min read
Node.js Automatic restart Node.js server with nodemon
We generally type following command for starting NodeJs server: node server.js In this case, if we make any changes to the project then we will have to restart the server by killing it using CTRL+C and then typing the same command again. node server.js It is a very hectic task for the development process. Nodemon is a package for handling this rest
1 min read
Top 3 Best Packages Of Node.js that you should try being a Node.js Developer
Node.js is an open-source and server-side platform built on Google Chrome's JavaScript Engine (V8 Engine). Node.js has its own package manager called NPM( Node Package Manager) which has very useful and incredible libraries and frameworks that makes our life easier as a developer to work with Node.js. The 3 Best Packages of Node.js that you should
4 min read
Node.js Connect Mysql with Node app
Node.js is a powerful platform for building server-side applications, and MySQL is a widely used relational database. Connecting these two can enable developers to build robust, data-driven applications. In this article, we'll explore how to connect a Node.js application with a MySQL database, covering the necessary setup, configuration, and basic
2 min read
How to resolve 'node' is not recognized as an internal or external command error after installing Node.js ?
Encountering the error message ‘node’ is not recognized as an internal or external command after installing Node.js can be frustrating, especially when you’re eager to start working on your project. This error usually means that your system cannot find the Node.js executable because the path to it is not set correctly in your system's environment v
2 min read