Complete the code to append text to a file using Node.js.
import { appendFile } from 'fs/promises'; await appendFile('log.txt', [1]);
To add text to a file, you use appendFile with the filename and the string to add.
Complete the code to append 'Hello World' with a newline to 'output.txt'.
import { appendFile } from 'fs/promises'; await appendFile('output.txt', [1]);
Appending 'Hello World' with a newline means the string should end with '\n'.
Fix the error in the code to append text asynchronously.
import fs from 'fs'; fs.appendFile('data.txt', [1], (err) => { if (err) throw err; console.log('Saved!'); });
The appendFile method expects a string as the second argument. Using await here is incorrect because this is a callback style function.
Fill both blanks to append 'Log entry' with a newline to 'app.log' using promises.
import { [1] } from 'fs/promises'; await [2]('app.log', 'Log entry\n');
To append text using promises, import and use appendFile from 'fs/promises'.
Fill all three blanks to append 'Error: ' plus a variable message and newline to 'errors.log'.
import { [1] } from 'fs/promises'; const message = 'File not found'; await [2]('errors.log', [3] + message + '\n');
Use appendFile to add text. The string to append is 'Error: ' plus the message plus a newline.