TypeScript:
Reading a text file

How to:

Let’s read a text file in TypeScript using Node.js’s fs/promises module. We’ll keep this example simple: read a file named example.txt and log its content.

import { readFile } from 'fs/promises';

async function readTextFile(filePath: string) {
  try {
    const data = await readFile(filePath, 'utf8');
    console.log(data);
  } catch (error) {
    console.error(`Error reading file from disk: ${error}`);
  }
}

readTextFile('./example.txt');

Sample Output:

Hello, this is content from the file!

Deep Dive

Historically, file reading in Node.js was heavily callback-based, which could lead to a phenomenon known as “callback hell”. With the advent of Promises and async/await, this process became much more streamlined.

Besides fs/promises, there’s the older fs module that still works with callback patterns. There’s also the option of using stream processing with fs.createReadStream(), useful for large files due to lower memory consumption.

Implementation-wise, accessing the file system is an I/O operation and inherently slower than in-memory operations. That’s why asynchronous coding patterns are important — they help prevent blocking the main thread and allow Node.js to keep handling other tasks.

See Also

For a deeper dive into Node.js file system: