How to write to a file using TypeScript? Today we will present for you 2 ways of fs.writeFileSync() method to do it and you can choose one of them. Let’s go into detail.
Write to a file using TypeScript
First, you should know that you will need an ‘fs’ module for a file system to interact with a file. The node package provides the file system. If you want to use the ‘fs’ module, you have to download the node package because Javascript writes the node package. So if you want it to work with Typescript, you will have to download the node package for Typescript.
You can use this command below in the terminal to download the node package:
npm install --save-dev @types/node
If you download success, let’s learn how to write to a file with the ‘fs’ module.
I will show you two ways to write files: synchronous and asynchronous.
Use fs.writeFileSync()
The writeFileSync() method will help you to write the file synchronously.
Syntax:
fs.writeFileSync(file, data, encoding, mode, flag)
Parameters:
- file: Direction to your file
- data: The thing you want to write to your file
- encoding: Indicate the type of content in your file. Common is ‘utf-8’.
- mode: Default (0o666). Indicate the file mode.
- flag: Default:’w’. Indicate the specific flag used while writing the file.
Example:
import { readFileSync, writeFileSync } from "fs"; const animalList = [ "Wallaby", "Walrus", "Wasps", "Winter White Russian Hamster", "Wolves", "Common Wasps", "Chickaree" ]; const data = animalList.join("-"); console.log(readFileSync("./text.txt", "utf-8")); writeFileSync("./text.txt", data, "utf-8"); console.log(readFileSync("./text.txt", "utf-8"));
Output:
Hello From Learn Share IT
Wallaby-Walrus-Wasps-Winter White Russian Hamster-Wolves-Common Wasps-Chickaree
Here I use the readFileSync method to read the file. You can read more about how to read files here. Remember, the data you want to read to your file has to be a string or a buffer. So in the code above, I convert from an array to a string by the join() method.
Use fs.writeFileSync()
The fs.writeFileSync() method will help you to write files asynchronously.
Syntax:
fs.writeFileSync(path,flag,callback(err))
Parameters:
- file: Direction to your file
- data: The thing you want to write to your file
- encoding: Indicate the content in your file. Common is ‘utf-8’.
- mode: Default (0o666). Indicate the file mode.
- flag: Default:’w’. Indicate the specific flag used while writing the file.
- callback: The function that would be executed when the method executes.
- err: If the write file is not successful, the error will be thrown.
Example:
import { readFileSync, writeFile } from "fs"; const animalList = [ "Wallaby", "Walrus", "Wasps", "Winter White Russian Hamster", "Wolves", "Common Wasps", "Chickaree" ]; const data = animalList.join("-"); console.log(readFileSync("./text.txt", "utf-8")); writeFile("./text.txt", data, "utf-8", (err) => { if (err) { console.log("Write file error"); } console.log(readFileSync("./text.txt", "utf-8")); });
Output:
Hello From Learn Share IT
Wallaby-Walrus-Wasps-Winter White Russian Hamster-Wolves-Common Wasps-Chickaree
Summary
We have shown you how to write to a file using TypeScript in this tutorial. You can use the writeFileSync or writeFile method provided by the’ fs’ module. Thank you for reading!
Maybe you are interested:
- How to parse a JSON string in TypeScript
- Import a JavaScript file into a TypeScript file
- How to read a File’s contents in TypeScript

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