node js delete file from directory code example

Example 1: nodejs fs delete file

const fs = require('fs')

const path = './file.txt'

try {
  fs.unlinkSync(path)
  //file removed
} catch(err) {
  console.error(err)
}

Example 2: delete with unlinksync node

const fs = require('fs')

const path = './file.txt'

try {
  fs.unlinkSync(path)
  //file removed
} catch(err) {
  console.error(err)
}

Example 3: how to delete a folder using node js

fs.unlink('./node/myText.txt',function(){
    fs.rmdir('node');
});