How to Rename File in Folder in Node.js?

19-Sep-2022

.

Admin

How to Rename File in Folder in Node.js?

Hello Dev,

This article details how to rename a file in a folder in node.js. you can understand how to rename the file in the folder in nodejs. you'll learn to rename file in a folder in nodejs. it's a simple example of rename a file in a folder in node js.

I will give you a simple example of rename file in the folder. In this example rename file in folder using fs (File System) package and rename() in node js.

So let's start following example:

Step 1: Install Node JS


This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:

mkdir my-app

cd my-app

npm init

Step 2: Update server.js file

server.js

var fs = require('fs');

fs.rename('./uploads/mypic-1659511827832.jpg', './uploads/user.jpg', function (err) {

if (err) throw err;

console.log('File Renamed Successfully.');

});

Output:

File Renamed Successfully.

I hope it can help you...

#Node JS