How to rename a directory in Node.js [3 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
3 min

banner

# Table of Contents

  1. How to rename a directory in Node.js
  2. Rename a directory in Node.js using fs.renameSync()
  3. Rename a directory in Node.js using callbacks with fs.rename()

# How to rename a directory in Node.js

To rename a directory in Node.js:

  1. Import the fs module from fs/promises.
  2. Use the fs.rename() method to rename the directory.
  3. The method takes the old path and the new path and renames the directory.
index.js
import fs from 'fs/promises'; // ๐Ÿ‘‡๏ธ If you use CommonJS require() // const fs = require('fs/promises'): async function renameDirectory(oldName, newName) { try { await fs.rename(oldName, newName); } catch (err) { console.log(err); } } const oldName = './old-dir-name'; const newName = './new-dir-name'; renameDirectory(oldName, newName).then(() => { console.log('directory renamed successfully'); });

rename directory in node using async await

The code for this article is available on GitHub

The code sample above uses the ES6 modules import/export syntax.

If you use the CommonJS require() syntax, use the following import statement instead.

index.js
const fs = require('fs/promises'):

The renameDirectory() function takes the old path to the directory and the new path as parameters and renames the directory.

The function uses the async/await syntax, so it returns a Promise that gets resolved once the directory has been renamed.

The fsPromises.rename() method takes 2 parameters:

  1. oldPath - the old path to the directory.
  2. newPath - the new path to the directory.

The method renames oldPath to newPath and returns a Promise that resolves with undefined upon success.

The example assumes that you want to rename a directory with an oldPath of ./old-dir-name to a newPath of ./new-dir-name.

index.js
renameDirectory(oldName, newName).then(() => { console.log('directory renamed successfully'); });

Alternatively, you can use the fs.renameSync() method.

# Rename a directory in Node.js using fs.renameSync()

You can also rename a directory synchronously in a similar way.

index.js
import fs from 'fs'; // ๐Ÿ‘‡๏ธ If you use the CommonJS require() syntax // const fs = require('fs') function renameDirectory(oldName, newName) { try { fs.renameSync(oldName, newName); } catch (err) { console.log(err); } } const oldName = './old-dir-name'; const newName = './new-dir-name'; renameDirectory(oldName, newName); console.log('Directory has been renamed successfully');

rename directory in node synchronously

The code for this article is available on GitHub

Notice that we imported the fs module from fs and not from fs/promises this time.

If you use the CommonJS require() syntax, use the following import statement instead.

index.js
const fs = require('fs')

The renameDirectory() function takes the oldPath and the newPath as parameters and renames the directory synchronously.

The function returns undefined as this is the return value of the fs.renameSync method.

# Rename a directory in Node.js using callbacks with fs.rename()

You can also use the fs.rename() method to rename a directory using the callback-style syntax.

index.js
import fs from 'fs'; // ๐Ÿ‘‡๏ธ If you use the CommonJS require() syntax // const fs = require('fs') function renameDirectory(oldName, newName) { fs.rename(oldName, newName, error => { if (error) { throw new Error(error); } }); } const oldName = './old-dir-name'; const newName = './new-dir-name'; renameDirectory(oldName, newName); console.log('Directory has been renamed successfully');

rename directory using fs rename

The code for this article is available on GitHub

We passed the following 3 arguments to the fs.rename() method:

  1. oldPath - the old path to the directory.
  2. newPath - the new path to the directory.
  3. a callback function that gets called with an error if the operation fails.

If renaming the directory succeeds the error parameter is null.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2025 Borislav Hadzhiev