Quick Tutorial: How to Write File using Typescript

--

Photo by Dominik Scythe on Unsplash

Writing a file is common operation in programming languages and platforms. Be it mobile, web, or backend, you write file for various reasons.

In this tutorial, you will learn how to write file using Typescript.

If you want to read the long version, please go here:
Mozzlog Write File in Typescript

Write File Synchronously

You can use the fs module to write file synchronously.

You can follow this example code:

import { writeFileSync } from "fs";

console.log("Start Write")
writeFileSync("file.txt", "My name is John", {
flag: "w"
})
console.log("End Write")

Write File Asynchronously

You can use the samefs module to write file asynchronously.

You can follow this example code:

import { promises } from "fs";

console.log("Start Write")
promises.writeFile("file.txt", "My name is Mozzlog", {
flag: "w"
}).then(() => {
console.log("Finish Write")
})
console.log("Continue Working")

Now you can write a file in Typescript quickly

--

--