How to Rename a Folder in Node.js?

Sep 16, 2022

.

Admin

How to Rename a Folder in Node.js?

I am going to show you an example of how to rename a folder in node.js. This article will give you a simple example of how to rename a folder in node js. we will help you to give an example of rename a folder in nodejs. This post will give you a simple example of rename a folder in node.js. So, let's follow a few step to create an example of rename a folder in node js.

I will give you a simple example of rename a folder in nodejs. In this example name folder using fs (File System) package 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

const fs = require("fs")

// Your Current Folder Name

const currPath = "./lib"

// Your New Folder Name

const newPath = "./db-lib"

// Check Your Folder Path

fs.rename(currPath, newPath, function(err) {

if (err) {

console.log(err)

} else {

console.log("Successfully rename the directory.")

}

})

Output:

Successfully rename the directory.

I hope it can help you...

#Node JS