Complete the code to import the Node.js file system module.
const fs = require([1]);The fs module is the built-in Node.js module to access the file system. You import it with require("fs").
Complete the code to read a file asynchronously using the fs module.
fs.readFile('example.txt', 'utf8', (err, data) => { if (err) throw err; console.log([1]); });
err instead of data.fs or console which are not file content.The data parameter contains the file content read by fs.readFile. You log data to see the file content.
Fix the error in the code to write data to a file asynchronously.
fs.writeFile('output.txt', [1], (err) => { if (err) throw err; console.log('File saved!'); });
fs or console.log instead of the data string.undefined which causes an error.The second argument to fs.writeFile must be the data to write. Using data correctly writes the content.
Fill both blanks to create a directory and then read its contents.
fs.[1]('myFolder', (err) => { if (err) throw err; fs.[2]('myFolder', (err, files) => { if (err) throw err; console.log(files); }); });
readFile which reads file content, not directory contents.unlink which deletes files.mkdir creates a directory. readdir reads the contents of a directory. Together they create and list files in 'myFolder'.
Fill all three blanks to check if a file exists, then read it if it does.
fs.[1]('data.txt', (err) => { if (!err) { fs.[2]('data.txt', 'utf8', (err, [3]) => { if (err) throw err; console.log([3]); }); } else { console.log('File does not exist'); } });
writeFile instead of readFile to read content.fs.access checks if the file exists. If no error, fs.readFile reads the file. The content is stored in data and logged.