ItSolutionStuff.com

How to Rename File in Folder using Node JS?

By Hardik Savani β€’ September 20, 2021
Node JS

Hi,

In this short tutorial we will cover an how to rename file in node js. let’s discuss about node js rename file if exists. you can understand a concept of node js rename files in folder. if you have question about node js rename file sync then i will give simple example with solution. Follow bellow tutorial step of node js fs rename file example.

We will use fs npm package for rename file in folder using node.js. fs package provide rename() for renaming file. let's see simple example

Step 1: Create Node App

run bellow command and create node app.

mkdir my-app

cd my-app

npm init

Step 2: Create server.js file

Make sure, you have add file on "uploads/laravel-doesnothave-users.png" path.

server.js

const fs = require('fs');

const filePath = './uploads/laravel-doesnothave-users.png';

const filePathRename = './uploads/laravel-doesnothave-users-RENAME.png';

fs.rename(filePath, filePathRename, function(err) {

if ( err ) console.log('ERROR: ' + err);

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

});

now you can simply run by following command:

node server.js

Output:

i hope it can help you...

Tags: Node JS
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

Node JS - How to Delete All Files in Directory?

Read Now β†’
β˜…

How to Check File is Exist or Not in Node JS?

Read Now β†’
β˜…

How to Create Directory if does not Exists in Node JS?

Read Now β†’
β˜…

How to Delete File If Exists in Node JS?

Read Now β†’
β˜…

How to Get All Files from Folder in Node JS?

Read Now β†’
β˜…

How to Download Image from URL in Node JS?

Read Now β†’
β˜…

Node JS Express Route with Parameters Example

Read Now β†’
β˜…

How to Remove Element from Array in Node JS?

Read Now β†’
β˜…

How to Push Element in Array in Node JS?

Read Now β†’
β˜…

Node JS Sort Array of Objects by Value Example

Read Now β†’
β˜…

How to run node js server on https using self-signed certificate?

Read Now β†’