delete a directory in javascript

To delete a directory in Node.js, we can use the built-in fs module that provides a rmdir() method. The rmdir() method removes the specified directory by path.

Here is an example code that shows how to delete a directory using Node.js:

index.tsx
const fs = require('fs');
const directoryPath = 'path/to/directory';

fs.rmdir(directoryPath, { recursive: true }, (err) => {
  if (err) {
    console.error(`Error while deleting directory ${directoryPath}: ${err.message}`);
  } else {
    console.log(`Directory ${directoryPath} is deleted successfully!`);
  }
});
316 chars
11 lines

In the above code, we use the rmdir() method to delete the directoryPath. The { recursive: true } options delete the directory recursively, including all files and subdirectories. If an error occurs, we display an error message. Otherwise, we display a success message.

gistlibby LogSnag