How to rename file in Node.js? fs.rename() Node.js

Introduction

In this blog post, we will learn how to rename a file in Node.js using fs module. fs mdoule provides various synchronous and asynchronous APIs to rename files. Let’s get start.

fs.rename()

The fs.rename() is used to asynchronously rename a file at the given old path to provided new path. In the case that a new path already exists, it will be overwritten.

Syntax of fs.rename()

fs.rename(oldPath, newPath, callback)

This method accept three parameters as described below:

  • oldPath: The path of the file that has to be renamed. It could be a string, buffer or URL.
  • newPath: The new path of the file has to be renamed. It could be a string, buffer or URL.
  • callback: It is the function that will be called when the renaming operation is done.
    • err: error object. (If there is no error, error object holds null value)

To rename a file using rename() use following code snippet:

const fs = require('fs');
 
fs.rename('foo.txt', 'bar.txt', function (err) {
  if (err) throw err;
  console.log('File rename operation completed.');
});

fs.renameSync()

The fs.renameSync() is used to synchronously rename a file at the given old path to provided new path. In the case that a new path already exists, it will be overwritten.

Syntax of fs.renameSync()

fs.renameSync(oldPath, newPath)

This method accept two parameters as described below:

  • oldPath: The path of the file that has to be renamed. It could be a string, buffer or URL.
  • newPath: The new path of the file has to be renamed. It could be a string, buffer or URL.

To rename a file using renameSync() use following code snippet:

const fs = require('fs');
 
fs.renameSync('foo.txt', 'bar.txt');
console.log('File rename operation completed.');

Conclusion

Here, we have seen how we can rename files in Node.js. It is advisable to use asynchronous function instead of synchronous. As synchronous function blocks the event loop and should not be used in the production environment,

#Happy Coding #Topcode

#rename file node js #rename file using node js #copy and rename file node js #node js rename file if exists #node js rename file sync #node js rename file extension #rename a file node js #rename a file in node js #rename file in nodejs #rename file nodejs example #node js fs rename file #how to rename file in node js #rename file in node js #rename file name in node js #rename file nodejs #rename file with node js #fs rename file nodejs #fs.rename nodejs #fs.rename #fs.renamesync example #Renaming files using node.js #Node.js — How to Rename a File #Renaming a file with Node.js #fs.rename JavaScript and Node.js code examples

, , , , ,

Related posts

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site