Reading and renaming files using Node.js modules like fs is common in CLI tools and backend scripts. When using the newer node: protocol style (e.g., require(“node:fs”)), you might face errors in older Node.js versions.
node:fs is a modern specifier that explicitly refers to Node.js core modules. It helps differentiate built-in modules from user-installed ones (e.g., via npm).
const { rename } = require("node:fs");
This syntax only works in Node.js v14.18.0+ and v16+.
Older Node versions will throw:
Error: Cannot find module 'node:fs'
For backward compatibility with older Node.js versions (v10, v12, early v14), use this instead:
const fs = require("fs");
fs.rename('./1.txt', './2.txt', (err) => {
if (err) throw err;
console.log('File renamed successfully');
});
If you want to use the modern node:fs import style:
Work with our skilled Node developers to accelerate your project and boost its performance.
3rd Floor, Aval Complex, University Road, above Balaji Super Market, Panchayat Nagar Chowk, Indira Circle, Rajkot, Gujarat 360005.
Abbotsford, BC
15th B Street 103, al Otaiba Dubai DU 00000, United Arab Emirates
3rd Floor, Aval Complex, University Road, above Balaji Super Market, Panchayat Nagar Chowk, Indira Circle, Rajkot, Gujarat 360005.
Abbotsford, BC
15th B Street 103, al Otaiba Dubai DU 00000, United Arab Emirates
Copyright © 2026 Niotechone Software Solution Pvt. Ltd. All Rights Reserved.