Node.js – How to rename a File | fs.rename()

  • by
node-js-fs-rename-how-to-rename-a-file.png

In this post we will learn different ways to rename a file in Node.js. Built-in module fs in Node.js helps us to work with file system. Using fs module functions we can create, delete, rename a file, folder or a directory. There are two different functions available to rename a file in Node.js namely fs.rename() and fs.renameSync(), the first function is asynchronously and the later is a synchronous function.

fs.rename() – Rename a file:

fs.rename() functions accepts 3 parameters oldPath – file path and its name which needs to be renamed, newPath – file path and file’s new name and a callback function.
Example:

const fs = require("fs")

let fileName = './media/images/wallpaper2.png';
let newFileName = './media/images/wallpaper1.png';

fs.rename(fileName, newFileName, function(err) {
if (err) {
console.log(err);
return;
}
console.log("File renamed successfully");
});

This code will rename a file with name wallpaper1.png to newFileName i.e. wallpaper2.png. If there is any error in the process, error will be logged or else success message will be logged.

Output:

File renamed successfully

Error Output:

[Error: ENOENT: no such file or directory, rename 'C:\Code\Node\fileSystem\media\images\wallpaper1.png' -> 'C:\Code\Node\fileSystem\media\images\wallpaper2.png'] {
errno: -4058,
code: 'ENOENT',
syscall: 'rename',
path: 'C:\\Code\\Node\\fileSystem\\media\\images\\wallpaper1.png',
dest: 'C:\\Code\\Node\\fileSystem\\media\\images\\wallpaper2.png'
}

Code Snapshot:
node-js-fs-rename-how-to-rename-a-file.png

DOWNLOAD SOURCE CODE

fs.renameSync() – Synchronous rename function:

fs.renameSync() is a synchronous rename function in Node.js fs module which takes only 2 parameter oldPath & newPath. It does not have any callback function. It will block the code execution until the rename operation is completed or it throws error.

Example:

const fs = require("fs")
let fileName = './media/images/wallpaper2.png';
let newFileName = './media/images/wallpaper3.png';
try {
fs.renameSync(fileName, newFileName);
console.log("File renamed successfully");
} catch (err) {
console.log(err);
}

Output:

File renamed successfully

Error Output:

Error: ENOENT: no such file or directory, rename './media/images/wallpaper2.png' -> './media/images/wallpaper3.png'
at Object.renameSync (node:fs:980:3)
at Object.<anonymous> (C:\Code\Node\fileSystem\file-rename.js:8:8)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
errno: -4058,
syscall: 'rename',
code: 'ENOENT',
path: './media/images/wallpaper2.png',
dest: './media/images/wallpaper3.png'
}

DOWNLOAD SOURCE CODE

We have also made a tutorial on:

Renaming a folder or a directory in Node.js

How to create directory using Node.js

Node.js – Create, read and write text files.

HTTP module in Node.js

Install Node.js on Windows.

Node.js – How to create PDF documents.