Last updated: Apr 5, 2024
Reading timeยท3 min
fs.renameSync()
fs.rename()
To rename a directory in Node.js:
fs
module from fs/promises
.fs.rename()
method to rename the directory.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'); });
The code sample above uses the ES6 modules import/export syntax.
If you use the CommonJS require()
syntax, use the following import statement
instead.
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:
oldPath
- the old path to the directory.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
.
renameDirectory(oldName, newName).then(() => { console.log('directory renamed successfully'); });
Alternatively, you can use the fs.renameSync()
method.
fs.renameSync()
You can also rename a directory synchronously in a similar way.
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');
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.
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.
fs.rename()
You can also use the fs.rename()
method to rename a directory using the
callback-style syntax.
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');
We passed the following 3 arguments to the fs.rename() method:
oldPath
- the old path to the directory.newPath
- the new path to the directory.If renaming the directory succeeds the error parameter is null
.
You can learn more about the related topics by checking out the following tutorials: