{"id":12256,"date":"2025-05-07T10:52:37","date_gmt":"2025-05-07T10:52:37","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=12256"},"modified":"2025-05-19T12:14:32","modified_gmt":"2025-05-19T12:14:32","slug":"check-if-file-exists-in-node-js","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/node\/check-if-file-exists-in-node-js","title":{"rendered":"Node.js Check If File Exists"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>In Node.js, checking whether a file exists is a common operation in file handling. This document explains different methods to check file existence using both synchronous and asynchronous approaches.<\/p>\n<h2>Prerequisites<\/h2>\n<p>Before proceeding, ensure that you have Node.js installed on your system. You can verify this by running:<br \/>\n<code>node -v<\/code><\/p>\n<h2>Methods to Check File Existence<\/h2>\n<h3>1. Using fs.existsSync (Synchronous Method)<\/h3>\n<p>The fs.existsSync method is a simple way to check if a file exists synchronously.<\/p>\n<p><strong>Implementation:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">const fs = require('fs');\r\nconst path = require('path');\r\nconst filePath = path.join(__dirname, 'example.txt');\r\n\r\nif (fs.existsSync(filePath)) {\r\n    console.log('File exists');\r\n} else {\r\n    console.log('File does not exist');\r\n}\r\n<\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<ul>\n<li>fs.existsSync(filePath) checks if the file exists.<\/li>\n<li>The method returns true if the file exists; otherwise, it returns false.<\/li>\n<li>This method is blocking and should not be used in performance-critical applications.<\/li>\n<\/ul>\n<div class=\"qanda-read-box\"><div class=\"bg-light read-more-icon\"><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/04\/24061434\/read-txt.png\" alt=\"Also Read\"><p><\/p><h3>Also Read:<\/h3><a href=\"https:\/\/www.bacancytechnology.com\/blog\/node-js-multer\" target=\"_blank\">Node JS Multer<\/a><\/div><\/div>\n<h3>2. Using fs.promises.access (Asynchronous Method)<\/h3>\n<p>The fs.promises.access method provides an asynchronous way to check file existence.<br \/>\n<strong>Implementation:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"oracledb\">const fs = require('fs').promises;\r\nconst path = require('path');\r\nconst filePath = path.join(__dirname, 'example.txt');\r\n\r\nasync function checkFile() {\r\n    try {\r\n        await fs.access(filePath);\r\n        console.log('File exists');\r\n    } catch (error) {\r\n        console.log('File does not exist');\r\n    }\r\n}\r\ncheckFile();\r\n\r\n<\/pre>\n<p><b>Explanation:<\/b><\/p>\n<ul>\n<li>fs.access(filePath) checks if the file exists.<\/li>\n<li>If the file exists, no error is thrown, and &#8220;File exists&#8221; is logged.<\/li>\n<li>If the file does not exist, an error is caught in the catch block, logging &#8220;File does not exist&#8221;.<\/li>\n<li>This method is non-blocking and recommended for asynchronous applications.<\/li>\n<\/ul>\n<h2>Choosing the Right Method<\/h2>\n<table class=\"table table-striped\">\n<tbody>\n<tr>\n<td><b>Method<\/b><\/td>\n<td><b>Blocking<\/b><\/td>\n<td><b>Recommended for<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">fs.existsSync<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Yes<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Simple, quick checks in scripts<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">fs.promises.access<\/span><\/td>\n<td><span style=\"font-weight: 400;\">No<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Asynchronous applications<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Conclusion<\/h2>\n<p>Both synchronous and asynchronous methods are available to check if a file exists in Node.js. While fs.existsSync is straightforward, fs.promises.access is preferred for non-blocking operations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In Node.js, checking whether a file exists is a common operation in file handling. This document explains different methods to check file existence using both synchronous and asynchronous approaches. Prerequisites Before proceeding, ensure that you have Node.js installed on your system. You can verify this by running: node -v Methods to Check File Existence [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12257,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-12256","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12256"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=12256"}],"version-history":[{"count":5,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12256\/revisions"}],"predecessor-version":[{"id":12385,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12256\/revisions\/12385"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/12257"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=12256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=12256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=12256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}