How to Check File is Exist or Not in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Delete File If Exists in Node JS?
- How to Get All Files from Folder in Node JS?
- How to Download Image from URL in Node JS?
- How to Generate QR Code in Node JS?
- Node JS Send Email with Attachment Example
- How to Push Element in Array in Node JS?
- How to use Underscore JS in Node JS App?
- Node JS Make HTTP Put Request Example
- How to run node js server on https using self-signed certificate?
- Node JS Express Form Submission Example