How to Check File is Exist or Not in Node JS?

By Hardik Savani September 16, 2021 Category : Node JS

Now, let's see article of node js check file exists or not. i would like to show you nodejs check file exists. you will learn how to check file is exist or not in node js. it's simple example of how to check whether file exists or not in node js.

We will use fs npm package for check file is exists or not on given path using node.js. fs package provide exists() for checking file is exists or not. let's see simple example

Step 1: Create Node App

run bellow command and create node app.

mkdir my-app

cd my-app

npm init

Step 2: Create server.js file

server.js

const fs = require('fs');

const filePath = './uploads/laravel-doesnothave-users.png';

fs.exists(filePath, function(exists) {

if(exists) {

console.log('File is exists.');

} else {

console.log('File not found.');

}

});

now you can simply run by following command:

node server.js

Output:

If file will exists on given path then it will give you following message.

i hope it can help you...

Tags :
Shares