# How To Write To A File Using Typescript

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()

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("-");
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()

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("-");

writeFile("./text.txt", data, "utf-8", (err) => {
if (err) {
console.log("Write file error");
}

});

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: