Node.js fs.rename() Method - GeeksforGeeks

13 Oct, 2021
The fs.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.
Syntax:
fs.rename( oldPath, newPath, callback )
Parameters: This method accept three parameters as mentioned above and described below:
oldPath: It holds the path of the file that has to be renamed. It can be a string, buffer or URL.
newPath: It holds the new path that the file has to be renamed. It can be a string, buffer or URL.
callback: It is the function that would be called when the method is executed. It has an optional argument for showing any error that occurs during the process.
Below examples illustrate the fs.rename() method in Node.js:
Example 1: This example uses fs.rename() method to rename a file.
// Node.js program to demonstrate the    // fs.rename() method      // Import filesystem moduleconst fs = require('fs');   // List all the filenames before renaminggetCurrentFilenames();   // Rename the filefs.rename('hello.txt', 'world.txt', () => {  console.log("\nFile Renamed!\n");     // List all the filenames after renaming  getCurrentFilenames();});   // Function to get current filenames// in directoryfunction getCurrentFilenames() {  console.log("Current filenames:");  fs.readdirSync(__dirname).forEach(file => {    console.log(file);  });}
Output:
Current filenames:
hello.txt
index.js

File Renamed!

Current filenames:
index.js
world.txt
Example 2: This example uses fs.rename() method to demonstrate an error during file renaming.
// Node.js program to demonstrate the    // fs.rename() method      // Import filesystem moduleconst fs = require('fs');   // List all the filenames before renaminggetCurrentFilenames();   // Rename the filefs.rename('hello.txt', 'geeks.txt', (error) => {  if (error) {          // Show the error     console.log(error);  }  else {      // List all the filenames after renaming    console.log("\nFile Renamed\n");       // List all the filenames after renaming    getCurrentFilenames();  }});   // Function to get current filenames// in directoryfunction getCurrentFilenames() {  console.log("Current filenames:");  fs.readdirSync(__dirname).forEach(file => {    console.log(file);  });}
Output:
Current filenames:
index.js
package.json
world.txt
[Error: ENOENT: no such file or directory, rename 
'G:\tutorials\nodejs-fs-rename\hello.txt' ->
'G:\tutorials\nodejs-fs-rename\geeks.txt'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'rename',
  path: 'G:\\tutorials\\nodejs-fs-rename\\hello.txt',
  dest: 'G:\\tutorials\\nodejs-fs-rename\\geeks.txt'
}
Reference: https://nodejs.org/api/fs.html#fs_fs_rename_oldpath_newpath_callback
Node.js-Methods
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
How to read and write Excel file in Node.js ?
Express.js res.render() Function
How to use an ES6 import in Node.js?
Roadmap to Become a Web Developer in 2022
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
Convert a string to an integer in JavaScript