How to Rename a Folder in Node.js

The simple way is to rename a folder or directory using fs.rename() synchronously and asynchronously. In this tutorial, you will learn how to rename folder in node js.

How to Rename a Directory or Folder in Node.js

Here are three common methods:

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:

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

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.

Recommended Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *