ItSolutionStuff.com

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

By Hardik Savani • September 16, 2021
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: Node JS
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Delete File If Exists in Node JS?

Read Now →

How to Get All Files from Folder in Node JS?

Read Now →

How to Download Image from URL in Node JS?

Read Now →

How to Generate QR Code in Node JS?

Read Now →

Node JS Send Email with Attachment Example

Read Now →

How to Push Element in Array in Node JS?

Read Now →

How to use Underscore JS in Node JS App?

Read Now →

Node JS Make HTTP Put Request Example

Read Now →

How to run node js server on https using self-signed certificate?

Read Now →

Node JS Express Form Submission Example

Read Now →