Promises with fs/promises
fs/promises gives promise versions of the file functions, which work well with async and await.
Example
const fs = require('node:fs/promises');
const os = require('node:os');
const path = require('node:path');
(async () => {
const f = path.join(os.tmpdir(), 'node_demo.txt');
await fs.writeFile(f, 'line1\nline2\n');
await fs.appendFile(f, 'line3\n');
const text = await fs.readFile(f, 'utf8');
console.log(text.trim().split('\n'));
await fs.unlink(f);
})();Result
[ 'line1', 'line2', 'line3' ]
Exercise
Add text to a file.
await fs.(f, "x")
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.