How to Remove a File in Node.js?

Sep 20, 2022

.

Admin

How to Remove a File in Node.js?

Hello Friends,

This tutorial will provide an example of how to remove a file in node.js. we will help you to give example of how to remove a file in nodejs. I’m going to show you about remove a file in nodejs. I explained simply about delete a file in node js.

I will give you a simple example of remove a file in nodejs. In this example delete file using fs (File System) package and unlink() in nodejs.

So let's start following example:

Step 1: Install Node JS


This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:

mkdir my-app

cd my-app

npm init

Step 2: Update server.js file

server.js

const fs = require('fs');

// Remove File

fs.unlink('/user.jpg', function (err) {

if (err) {

console.error(err);

console.log('File Not Found!');

}else{

console.log('File Delete Successfuly!');

}

});

Output:

File Delete Successfuly!

I hope it can help you...

#Node JS