How do you read a file in Node?

Node.js modules, files and async · Freshers

Model answer

Use the promise version of fs: import { readFile } from 'node:fs/promises'; const text = await readFile('data.txt', 'utf8'). Avoid readFileSync inside request handlers because it blocks the thread. For large files use streams.

Learn this topic

Async JavaScript in Node: Promises, async/await and the Event Loop in the course Node.js and Express: Build a Back End API from Scratch.

Sign in to add your own answer.

More questions