How to use the fs module in TypeScript?

Sometimes, we want to use the fs module in TypeScript.

in this article, we’ll look at how to use the fs module in TypeScript.

How to use the fs module in TypeScript?

To use the fs module in TypeScript, we can import the module with import.

For instance, we write

import * as fs from "fs";
import * as path from "path";

fs.readFile(
  path.join(__dirname, "./client/index.html"),
  "utf8",
  (error, data) => {
    // ...
  }
);

to import the fs module with

import * as fs from "fs";

Then we call fs.readFile with the path of the file we want to read.

Then we get the file data from the data parameter in the callback.