How to Read Text File in TypeScript

Migel Hewage Nimesha Feb 02, 2024
  1. TypeScript With node.js
  2. the Node Module
  3. the fs Module
  4. Read a File With fs Module
How to Read Text File in TypeScript

This tutorial is about how to read text files in TypeScript.

TypeScript With node.js

TypeScript is a superset of JavaScript. While it inherits all the features from JavaScript itself, It provides many powerful features out of the box that is not included in JavaScript.

The node.js is a JavaScript framework that enables us to write JavaScript on the server-side. It made a considerable difference to the typical browser-based JavaScript execution norm.

To develop with node.js and TypeScript, you must have node.js installed. You can check whether the Node has been installed or not using the following command.

node -v

The above command should display the currently installed node.js version. Else, you need to install node.js from the official site.

Furthermore, your machine should have a TypeScript compiler package. Verify it by running the following command.

tsc --version

It should display the TypeScript compiler version.

the Node Module

With the node.js inclusion, TypeScript can utilize the NPM packages without hassle. NPM provides thousands of open source packages and libraries that any application can use to implement specific functionalities in no time.

The node.js packages are written in JavaScript language. Hence, you must install the type definitions for NPM packages in advance.

Let’s install the node type definitions as shown in the following.

npm install --save-dev @types/node

It would install all the type definitions for node packages.

the fs Module

The fs module is a widely known package used to interact with the file system. It offers a wide range of features to deal with a local file system, such as reading from a file, writing to a file, removing a directory, appending to a file, etc.

Read a File With fs Module

The fs module supports both the asynchronous and synchronous APIs to read from a file.

readFileSync(path, [encoding])

or

readFile(path, [encoding], callback_function)

Use the readFileSync() Function in TypeScript

The readFileSync() function can read a file synchronously. It blocks the execution until the file read operation finishes.

Let’s import the fs module first. Usually, node.js uses require() to import packages.

Since we use typescript here, we can use import.

import * as fs from 'fs';

This would import all the functions within the fs package, and we can call them by fs.

Next, we will initiate a variable to hold the file path.

const fileName: string = 'example.txt';

Finally, we will call the readFileSync() method with the parameters.

let fileContent = fs.readFileSync(fileName, 'utf8');
console.log(fileContent);

The encoding type parameter should be set to utf8. Then the method will return the file content as a string.

Else, the file content is returned as a buffer value.

Output:

Read file image in Typescript

As expected, the file content has been logged to the console.

Use the readFile() Function in TypeScript

The readFile() method can read a system file asynchronously. Hence, we can pass the callback function parameter to this function.

Let’s import the fs and initialize a variable to hold the file path.

import * as fs from 'fs';
const fileName: string = 'example.txt';

Use the readFile method, as shown in the following.

fs.readFile(fileName, 'utf8', (err, data)=> {
    console.log(data);
});

You could notice the same output as in the previous example.

Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.

Related Article - TypeScript File