Node.js ships with a file system module allowing you to interact with files on the local hard disk. The file system module fs provides two methods allowing you to rename files: Fs#rename
and Fs#renameSync
.
This tutorial shows you how to rename a file synchronously and asynchronously using Node.js’ fs
core module.
Node.js Series Overview
- String Replace All Appearances
- Remove All Whitespace From a String in JavaScript
- Generate a Random String in Node.js or JavaScript
- Remove Extra Spaces From a String in JavaScript or Node.js
- Remove Numbers From a String in JavaScript or Node.js
- Get the Part Before a Character in a String in JavaScript or Node.js
- Get the Part After a Character in a String in JavaScript or Node.js
- How to Check if a Value is a String in JavaScript or Node.js
- Check If a String Includes All Strings in JavaScript/Node.js/TypeScript
- Check if a Value is a String in JavaScript and Node.js
- Limit and Truncate a String to a Given Length in JavaScript and Node.js
- How to Run an Asynchronous Function in Array.map()
- How to Reset and Empty an Array
- for…of vs. for…in Loops
- Clone/Copy an Array in JavaScript and Node.js
- Get an Array With Unique Values (Delete Duplicates)
- Sort an Array of Integers in JavaScript and Node.js
- Sort a Boolean Array in JavaScript, TypeScript, or Node.js
- Check If an Array Contains a Given Value in JavaScript or Node.js
- Add an Item to the Beginning of an Array in JavaScript or Node.js
- Append an Item at the End of an Array in JavaScript or Node.js
- How to Exit and Stop a for Loop in JavaScript and Node.js (Coming soon)
- Split an Array Into Smaller Array Chunks in JavaScript and Node.js
- How to Get an Index in a for…of Loop in JavaScript and Node.js (Coming soon)
- How to Exit, Stop, or Break an Array#forEach Loop in JavaScript or Node.js (Coming soon)
- Callback and Promise Support in your Node.js Modules
- Run Async Functions/Promises in Sequence
- Run Async Functions/Promises in Parallel
- Run Async Functions in Batches
- How to Fix “Promise resolver undefined is not a function” in Node.js or JavaScript
- Detect if Value Is a Promise in Node.js and JavaScript
- Get a File’s Created Date
- Get a File’s Last Modified/Updated Date
- How to Create an Empty File
- Check If a Path or File Exists
- How to Rename a File
- Check If a Path Is a Directory (Coming soon)
- Check If a Path Is a File (Coming soon)
- Retrieve the Path to the User’s Home Directory (Coming soon)
- How to Touch a File (Coming soon)
Asynchronously Rename Files in Node.js
Node.js’ fs
core module provides the Fs#rename
method as an asynchronous/non-blocking way to rename files on disk. Non-blocking in this case means Node.js processes other operations while waiting for the hard disk to respond.
This rename
supports callbacks by default. The callback support comes from the early days of Node.js where asynchronous operations were handled using callbacks.
Later, Node.js added support for promises and async/await. This tutorial assumes you’re using async/await for flow control of your code. That’s the reason the example uses the require('fs').promises
exports available in Node.js v10.0.0 and later. These promise-based file system methods are usable with async/await.
The Fs#rename
method expects two arguments:
- the old file path
- the new file path
At first, you may expect a file path and the new file name instead of two file paths as the arguments. Well, renaming a file is basically moving a file from one place to another. Even when not changing the directory. The underlying operation of renaming a file is the “move” from the previous name to the new one.
Here’s an example on how to rename a file in Node.js using the built-in fs
module:
const Path = require('path')
const { promises: Fs } = require('fs')
const oldPath = Path.join(__dirname, "oldFile.txt")
const newPath = Path.join(__dirname, "newFile.txt")
await Fs.rename(oldPath, newPath)
Please notice: this method overwrites any existing file at newPath
. In case the newPath
points to an existing directory, this method throws an error.
Synchronously Rename Files
Node.js also comes with an Fs#renameSync
method. This method blocks the Node.js event loop for other operations (because it waits for the hard disk) while renaming a file. Here’s an example showing you how to rename a file synchronously:
const Fs = require('fs')
const Path = require('path')
const oldPath = Path.join(__dirname, "oldFile.txt")
const newPath = Path.join(__dirname, "newFile.txt")
Fs.renameSync(oldPath, newPath)
Use the @supercharge/filesystem Package
I’m the maintainer of the @supercharge/filesystem package providing convenient file system utilities. Methods in the @supercharge/filesystem
package are async by default and don’t block the event loop.
You may use the rename
method supporting the same arguments as Node.js’ built-in method. In this case, the @supercharge/filesystem
package just passes the calls through to Node.js’ core module without adding extra handling:
const Path = require('path')
const Fs = require('@supercharge/filesystem')
const oldPath = Path.join(__dirname, "oldFile.txt")
const newPath = Path.join(__dirname, "newFile.txt")
await Fs.rename(oldPath, newPath)
Enjoy!
Mentioned Resources
- Node.js docs for the fs module
- @supercharge/filesystem repository on GitHub