delete a file in typescript

To delete a file in TypeScript, you can make use of the built-in fs module of Node.js. The fs (file system) module is a built-in module that provides file I/O functionality.

Here is an example code snippet that demonstrates how to delete a file using fs.unlink() method:

index.ts
import fs from 'fs';

const filePath = 'path/to/file.txt';

fs.unlink(filePath, (err) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log('File deleted successfully.');
});
193 chars
12 lines

In this code snippet, we first import the fs module in our TypeScript file. Then we declare the filePath variable which is the path of the file that we want to delete.

We then call fs.unlink() method by passing the filePath variable as the first argument. The second argument is a callback function that is called after the file is deleted. The callback function takes an error object as the argument. If there is an error during the deletion process, we log the error to the console. If there is no error, we log a success message to the console.

gistlibby LogSnag