replace the name of a file by another name in javascript

In Node.js, use the fs (file system) module to rename a file. Here's an example code snippet:

index.tsx
const fs = require('fs');

const oldPath = '/path/to/old/file.txt';
const newPath = '/path/to/new/file.txt';

fs.rename(oldPath, newPath, function (err) {
  if (err) throw err;
  console.log('File Renamed.');
});
213 chars
10 lines

In the above code, replace /path/to/old/file.txt with the path and name of the file that you want to rename and /path/to/new/file.txt with the new path and name of the file.

gistlibby LogSnag