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

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

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 lesson 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 lesson

Lesson 1: Create Node App

run bellow command and create node app.

mkdir my-app

cd my-app

npm init

Lesson 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

Result:-

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

thank you very much for following up with me.

Tags

Recent Posts

Related Posts