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');
});
This works on all versions of Node.js.
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.
Hire Node.js Developers