How to Rename a Directory in Node.js


To rename a folder in node js can be useful when working with file management, organizing data, or any other situation where you need to change the name of a directory.

So, In this tutorial, you will learn how to rename a directory or folder in node js using fs.rename(), fs.promises.rename(),fs-extra Library.

Node.js Rename a Directory Tutorial

Here are three common approaches to renaming a directory or folder in node.js; as follows:

  • Method 1: Using the fs.rename Method
  • Method 2: Using Promises with fs.promises.rename (Node.js 10.0.0 and later)
  • Method 3: Using the fs-extra libraryEzoic

Method 1: Using the fs.rename Method

The fs.rename method is a built-in function in the Node.js fs (File System) module, and it’s used to change the name of a directory in Node.js. To illustrate, here’s an example of how to rename a directory in Node.js:

const fs = require('fs');

const oldDirectoryName = 'old_directory_name';
const newDirectoryName = 'new_directory_name';

fs.rename(oldDirectoryName, newDirectoryName, (err) => {
if (err) {
console.error('Error renaming the directory:', err);
} else {
console.log('Directory renamed successfully!');
}
});

Method 2: Using Promises with fs.promises.rename (Node.js 10.0.0 and later)

If you like using Promises for handling asynchronous tasks, there’s a method called fs.promises.rename available in Node.js starting from version 10.0.0. You can use it like this:

Ezoic

const fs = require('fs').promises;

async function renameDirectory(oldDirectoryName, newDirectoryName) {
  try {
    await fs.rename(oldDirectoryName, newDirectoryName);
    console.log('Directory renamed successfully');
  } catch (err) {
    console.error(`Error renaming directory: ${err}`);
  }
}

renameDirectory('old_directory_name', 'new_directory_name');

Method 3: Using the fs-extra library

The fs-extra library is like an extra toolbox for dealing with files and folders in your computer. It has a special tool that makes it easy to change the names of folders. To use this library, you have to install it first by following these steps

Ezoic

npm install fs-extra

Here is an example of how to rename a directory in Node.js using fs extra library:

const fs = require('fs-extra');

const oldDirectoryName = 'old_directory_name';
const newDirectoryName = 'new_directory_name';

fs.move(oldDirectoryName, newDirectoryName, (err) => {
  if (err) {
    console.error(`Error renaming directory: ${err}`);
  } else {
    console.log('Directory renamed successfully');
  }
});

Conclusion

You’ve learned three methods to rename a directory in Node.js using the fs.rename(), fs.promises.rename(),fs-extra Library. Choose the method that best suits your project’s requirements and Node.js version.

Ezoic

Recommended Tutorials

Ezoic

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?