0
0
Node.jsframework~10 mins

Checking file existence and stats in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Node.js file system module.

Node.js
const fs = require([1]);
Drag options to blanks, or click blank then click option'
A"os"
B"fs"
C"http"
D"path"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'path' or 'http' instead of 'fs' in require.
Forgetting to put quotes around the module name.
2fill in blank
medium

Complete the code to check if a file exists synchronously.

Node.js
const exists = fs.[1]Sync('example.txt');
Drag options to blanks, or click blank then click option'
AexistsSync
Bexists
CreadFile
Dstat
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exists' which is deprecated.
Using 'readFile' which reads file content instead of checking existence.
3fill in blank
hard

Fix the error in the code to get file stats asynchronously.

Node.js
fs.stat('file.txt', ([1], stats) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(stats.size);
});
Drag options to blanks, or click blank then click option'
AerrorObj
Berror
Cerr
De
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the one checked inside the callback.
Forgetting the error parameter entirely.
4fill in blank
hard

Fill both blanks to create a synchronous check for file stats and print if it is a file.

Node.js
try {
  const stats = fs.[1]('data.json');
  if (stats.[2]()) {
    console.log('It is a file');
  }
} catch (err) {
  console.error('Error:', err);
}
Drag options to blanks, or click blank then click option'
AstatSync
BisFile
CisDirectory
Dstat
Attempts:
3 left
💡 Hint
Common Mistakes
Using asynchronous stat method without callback.
Using isDirectory() instead of isFile() when checking for a file.
5fill in blank
hard

Fill all three blanks to read a file asynchronously and log its size in bytes.

Node.js
fs.[1]('log.txt', ([2], data) => {
  if ([3]) {
    console.error([3]);
    return;
  }
  console.log('File size:', data.length);
});
Drag options to blanks, or click blank then click option'
AreadFile
Berr
Dstat
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'stat' instead of 'readFile' to read file content.
Using different names for error parameter inconsistently.