Node FS Rename File

In this tutorial we will learn how to rename a file synchronously and asynchronously in Node.js using fs module.

Node FS Rename File – To rename file with Node FS, use fs.rename(new_file_name, old_file_name, callback_function) for asynchronous file rename operation and use fs.renameSync(new_file_name, old_file_name) for synchronous file rename operation. In this Node.js Tutorial, we shall learn syntax and examples for fs.rename() and fs.renameSync() functions of Node FS module.

Syntax – fs.rename()

The syntax of rename() function is

</>
Copy
fs.rename(new_file_path, old_file_path, callback_function)

where

  • new_file_path (is a String and Mandatory) :  The new file path you would like to assign
  • old_file_path (is a String and Mandatory) : Path to the file whose name is to be changed
  • callback_function : When file renaming operation is done, Callback Function is called with an error object. (If there is no error, error object holds null value)

fs.rename() function renames files asynchronously.

Syntax – fs.renameSync()

The syntax of renameSync() function is

</>
Copy
fs.renameSync(new_file_path, old_file_path)

fs.renameSync() function renames files synchronously.

Example 1 – Rename file Asynchronously

In this example, we will rename a file asynchronously, from sample_old.txt to sample.txt.

To rename a file asynchronously in Node.js using Node FS, use rename() function as shown below.

nodejs-rename-file.js

</>
Copy
var fs = require('fs');

fs.rename('sample.txt', 'sample_old.txt', function (err) {
  if (err) throw err;
  console.log('File Renamed.');
});

Open a terminal or command prompt and run this script using node command as shown in the following.

Output

arjun@arjun-VPCEH26EN:~/nodejs$ node nodejs-rename-file.js 
File Renamed.

Please note that when you rename a file asynchronously, it is not guaranteed that the renaming happens immediately. And if you plan some tasks with the renamed file right after the renaming operation like reading file, deleting file etc., it may not execute as expected. So make a thumb rule

If there are no further tasks related to the file after renaming, rename file asynchronously, else rename it synchronously.

Synchronous Operation costs execution time. So, based on your requirement, plan to use either rename() or renameSync().

Example to Rename file Synchronously

In this example, we will rename a file synchronously, from sample_old.txt to sample.txt.

To rename a file synchronously in Node.js using Node FS, use renameSync() function as shown below.

nodejs-rename-file.js

</>
Copy
var fs = require('fs');

fs.renameSync('sample.txt', 'sample_old.txt');
console.log('File Renamed.');

Open a terminal or command prompt and run this script using node command as shown in the following.

Output

arjun@arjun-VPCEH26EN:~/nodejs$ node nodejs-rename-file.js 
File Renamed.

Conclusion

Node FS Rename File – We have learnt to rename a file synchronously and asynchronously with the help of examples using rename() and renameSync() functions of Node FS.