how to use the rename function from the fs-extra library in typescript

To use the rename function from the fs-extra library in TypeScript, you need to first install fs-extra as a dependency:

npm install --save fs-extra
28 chars
2 lines

Then, you can import the rename function and use it to rename a file like this:

index.ts
import * as fs from 'fs-extra';

fs.rename('/path/to/old-file.txt', '/path/to/new-file.txt', (err) => {
  if (err) {
    console.error(err);
  } else {
    console.log('File renamed!');
  }
});
194 chars
10 lines

The rename function takes in the path of the old file, path of the new file, and a callback function that will be called once the file has been renamed. The callback function takes an error object as an argument if there was an error during the rename process, otherwise it will be null.

gistlibby LogSnag