If file exists, err is null and stats object contains file info.
Step 2: Check stats properties
stats.isFile() returns true if it is a file, stats.size returns file size in bytes.
Final Answer:
true 1024 -> Option A
Quick Check:
File exists and is file = true and size = 1024 [OK]
Hint: stats.isFile() true means file exists, size shows bytes [OK]
Common Mistakes:
Assuming stats.size is undefined
Confusing isFile() with isDirectory()
Not handling error callback properly
4. Identify the error in this code snippet that checks if a file exists:
const fs = require('fs');
try {
fs.access('data.txt');
console.log('File exists');
} catch (err) {
console.log('File does not exist');
}
medium
A. fs.access does not check file existence
B. fs.access is asynchronous and needs a callback or promise
C. Try/catch cannot catch errors in Node.js
D. console.log syntax is incorrect
Solution
Step 1: Check fs.access usage
fs.access is asynchronous and requires a callback or promise to handle errors.
Step 2: Understand try/catch with async
Try/catch does not catch errors from async calls without await or callback handling.
Final Answer:
fs.access is asynchronous and needs a callback or promise -> Option B
Quick Check:
Async fs.access needs callback/promise [OK]
Hint: Async functions need callbacks or await, not try/catch alone [OK]
Common Mistakes:
Assuming try/catch works with async without await
Ignoring callback parameter in fs.access
Thinking fs.access does not check existence
5. You want to write a function that returns true if a given path is a directory and exists, false otherwise. Which code snippet correctly implements this using Node.js synchronous methods?
hard
A. function isDirectory(path) {
if (fs.statSync(path).isDirectory()) return true;
else return false;
}
B. function isDirectory(path) {
return fs.accessSync(path) && fs.statSync(path).isDirectory();
}
C. function isDirectory(path) {
try {
return fs.statSync(path).isDirectory();
} catch {
return false;
}
}
D. function isDirectory(path) {
try {
return fs.existsSync(path) && fs.statSync(path).isFile();
} catch {
return false;
}
}
Solution
Step 1: Check for existence and directory type safely
Using fs.statSync inside try/catch handles missing path errors and checks if it's a directory.
Step 2: Analyze other options
function isDirectory(path) {
return fs.accessSync(path) && fs.statSync(path).isDirectory();
} misuses fs.accessSync without error handling; function isDirectory(path) {
try {
return fs.existsSync(path) && fs.statSync(path).isFile();
} catch {
return false;
}
} checks isFile() instead of isDirectory(); function isDirectory(path) {
if (fs.statSync(path).isDirectory()) return true;
else return false;
} lacks error handling for missing path.
Final Answer:
function isDirectory(path) { try { return fs.statSync(path).isDirectory(); } catch { return false; } } -> Option C
Quick Check:
Try/catch with statSync and isDirectory() = correct [OK]
Hint: Use try/catch with fs.statSync and isDirectory() to check safely [OK]