0
0
Node.jsframework~10 mins

Checking file existence and stats in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Checking file existence and stats
Start
Call fs.stat(path)
Does file exist?
Yes No
Get stats
Use stats
End
This flow shows how Node.js checks if a file exists by calling fs.stat, then either gets file stats or handles the error if the file is missing.
Execution Sample
Node.js
import { stat } from 'node:fs/promises';

async function checkFile(path) {
  try {
    const stats = await stat(path);
    return stats.isFile();
  } catch {
    return false;
  }
}
This async function checks if a file exists at the given path and returns true if it is a file, false otherwise.
Execution Table
StepActionEvaluationResult
1Call stat(path)Does file exist at path?Yes or No
2If YesRetrieve stats objectstats object with file info
3Call stats.isFile()Is it a file?true or false
4If No (error thrown)Catch errorReturn false
5Return resulttrue if file, false if notFunction returns boolean
💡 Function ends after returning true or false based on file existence and type
Variable Tracker
VariableStartAfter stat callAfter catchFinal
path'some/path.txt''some/path.txt''some/path.txt''some/path.txt'
statsundefinedstats objectundefinedstats object or undefined
resultundefinedundefinedfalsetrue or false
Key Moments - 3 Insights
Why do we use try/catch around stat()?
Because stat() throws an error if the file does not exist, so try/catch lets us handle that case gracefully (see execution_table step 4).
What does stats.isFile() check?
It checks if the path points to a regular file, not a directory or other type (see execution_table step 3).
Why return false in the catch block?
Because an error means the file doesn't exist or can't be accessed, so returning false signals no valid file (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 3?
ARetrieve stats object
BCheck if stats is a file
CCatch error if file missing
DReturn false
💡 Hint
Refer to execution_table row with Step 3 describing stats.isFile() check
At which step does the function handle the file not existing?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table row 4 where catch handles errors
If the file exists but is a directory, what will the function return?
Afalse
Btrue
Cthrows error
Dundefined
💡 Hint
Check variable 'result' after stats.isFile() returns false for directories
Concept Snapshot
Use fs.stat(path) inside try/catch to check file existence.
If stat succeeds, use stats.isFile() to confirm it's a file.
If stat throws error, file doesn't exist; return false.
This async pattern safely checks file presence and type.
Full Transcript
This lesson shows how to check if a file exists and get its stats in Node.js using the fs/promises module. The key function is stat(path), which returns file info if the file exists or throws an error if it doesn't. We use try/catch to handle this error. If stat succeeds, we call stats.isFile() to check if the path is a file. The function returns true if it is a file, otherwise false. This approach avoids crashes and clearly signals file presence.