Rename a file synchronously using node.js


Rename a file synchronously using fs.renameSync method of fs module of node.js


//Name of the file : renamefile-sync.js
var fs =  require('fs');
//you have to pass the Relative path of the file from the Current working directory.
fs.renameSync('message.txt', 'new_data.txt');
console.log('File renamed successfully');

// To check it's Synchronous nature !
console.log("This method is Synchronous");												


  • Now run the snippet using the following command :
    >node renamefile-sync.js
    File renamed successfully
    This method is Synchronous