How to rename a file using JavaScript

In javascript-based test automation projects, if you need to rename a file using javascript, you can do that by using fs node package. These are very important for any Javascript-based test automation project. Let’s see how to delete a file using javascript

In order to use this fs module, you need to call it in your code like below.

var fs = require('fs');
How to rename a file using javascript- example code

As it is clear from the function argument names, there are three arguments i.e. Filepath, old name, and a new name. Once the file is renamed, console.log will print the message to notify the user about renaming the file.

async renameFile(filePath, existingName, newName){
    if (fs.existsSync(filePath+ existingName)) {
      fs.rename(filePath+ existingName, filePath + newName, function (err) {
        if (err) throw err;
        console.log('File Renamed!');


You may also read

Similar Posts

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.