Complete the code to import the Node.js file system module.
const fs = require([1]);The fs module is used to work with the file system in Node.js. You import it by requiring the string "fs".
Complete the code to check if a file exists synchronously.
const exists = fs.[1]Sync('example.txt');
The existsSync method checks if a file exists synchronously and returns true or false.
Fix the error in the code to get file stats asynchronously.
fs.stat('file.txt', ([1], stats) => { if (err) { console.error(err); return; } console.log(stats.size); });
The callback function receives an error object as the first argument, commonly named err. The code checks if (err) so the parameter must be named err to match.
Fill both blanks to create a synchronous check for file stats and print if it is a file.
try { const stats = fs.[1]('data.json'); if (stats.[2]()) { console.log('It is a file'); } } catch (err) { console.error('Error:', err); }
statSync gets file stats synchronously. The isFile() method checks if the path is a file.
Fill all three blanks to read a file asynchronously and log its size in bytes.
fs.[1]('log.txt', ([2], data) => { if ([3]) { console.error([3]); return; } console.log('File size:', data.length); });
readFile reads the file asynchronously. The first callback parameter is the error, commonly named err. The code checks and logs err if it exists.