Learning how to read a file’s contents in TypeScript will be very helpful when you work with API and want to read a file or write a file. So how to do it? Let’s go into detail now.
Read a file’s contents in TypeScript
To read a file’s contents, we will need support from a module called ‘fs’, which stands for the file system, to help you work with the file. NodeJS will be downloaded automatically, but we will have to download it manually with Typescript.
First, make sure you have NodeJS by this command:
node –v
If it shows something like this:
v16.17.0
It means you already have a node. If not, you can download it here.
Then you can download the node module by this command:
npm install --save-dev @types/node
If you download success, let’s read and start reading your file. The fs module can read a file in both synchronous and asynchronous ways. So we will go one by one way.
Use readFileSync()
The readFileSync() function will help you to read a file synchronously and return a string which is the content of your file.
Syntax:
fs.readFileSync(path, encoding, flag)
Parameters:
- path: The location of your file, which is the filename or file descriptor.
- encoding: The way the contents in your file are encoded. The most common is ‘utf-8’. If you do not pass these parameters, you might get an error.
- flag: You can pass in a flag to indicate what you want to read in the file.
Example:
import { readFileSync } from "fs"; const content: string = readFileSync("./text.txt", "utf-8"); console.log(content);
Hello From Learn Share IT
Output:
Hello From Learn Share IT
Use readFile()
The readFile() method will help you to read files asynchronously. You can pass a callback function that will execute when the read file end.
Syntax:
fs.readFile(path,flag,callback(err,data))
Parameters:
- path: The location of your file, which is the filename or file descriptor.
- encoding: The way the contents in your file are encoded. The most common is ‘utf-8’. If you do not pass these parameters, you might get an error.
- callback: callback which will be called when the read file end.
- error: The error when reading file unsuccess
- data: Content in the file is read
Example:
import { readFile } from "fs"; readFile("./text.txt", "utf-8", (err, data) => { console.log(data); });
Hello From Learn Share IT
Output:
Hello From Learn Share IT
Summary
In this tutorial, we have shown you how to read a file’s contents in TypeScript. To read the content, you can use the fs module and the readFileSync and readFile methods. If you have any problems, please comment below. We will respond as possible. Thanks for reading!
Maybe you are interested:
- How to write to a File using TypeScript
- How to parse a JSON string in TypeScript
- Import a JavaScript file into a TypeScript file

Hello, guys! I hope that my knowledge in HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, Python, MySQL, and npm computer languages may be of use to you. I’m Brent Johnson, a software developer.
Name of the university: HOU
Major: IT
Programming Languages: HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, PyThon, MySQL, npm