VOOZH about

URL: https://blog.logrocket.com/understanding-denos-file-system/

⇱ Understanding Deno’s file system - LogRocket Blog


2020-12-02
1205
#deno
Wisdom Ekpot
29893
👁 Image

See how LogRocket's Galileo AI surfaces the most severe issues for you

No signup required

Check it out

Deno comes with its own file system as a core module. This file system module can be used for any kind of file or directory manipulation.

👁 Understanding Deno’s File System

In this tutorial, we’ll go through the file system methods you can use in your Deno application.

Presently, some methods are still experimental and quite unstable; you should always add the --unstable flag when using these methods.

We’ll walk you through the following:

🚀 Sign up for The Replay newsletter

The Replay is a weekly newsletter for dev and engineering leaders.

Delivered once a week, it's your curated guide to the most important conversations around frontend dev, emerging AI tools, and the state of modern software.

How to write files in Deno

There are several ways to write a file in Deno. They all require the --allow-write flag and will throw an error if one occurs.

You can use Deno.writeTextFile or Deno.writeFile to write text into a file. Deno.writeTextFile comes in both synchronous and asynchronous format and takes in two parameters: the location of the file and the content to be written into the file below.

// using the async method
await Deno.writeTextFile('./file.txt', 'This is the content to be written');

//using sync method
Deno.writeTextFileSync('./file.txt', 'This is the content to be written');

You can also use the Deno.writeFile method with a TextEncoder. The TextEncoder converts strings to Uint8Array:

const encoder = new TextEncoder(); // to convert a string to Uint8Array
await Deno.writeFile("./file.txt", encoder.encode("Content to be written"));

Finally, you can use the Deno.open and Deno.writeAll methods to open a file, write content to it, and then close the file, as shown below.

const file = await Deno.open("./image.png", { write: true, create: true }); //this opens the file
await Deno.writeAll(file, imageBytes); //writes to file
file.close(); // closes the file

If you try to write to a file that does not exist, Deno automatically creates the file.

How to read files in Deno

Like writing files, there are myriad ways to read files in Deno, all of which require the --allow-read flag.

You can use the Deno.readTextFile and Deno.readFile methods to read a file in Deno. Deno.readTextFile comes in both synchronous and asynchronous format and takes in the file path as a parameter:

// using the async method
const text = await Deno.readTextFile("file.txt");
console.log(text);

//using the sync method
const sync = Deno.readTextFileSync("file.txt");
console.log(sync);

Another method is Deno.readFile. You must first decode the file to a readable format using the TextDecoder method.

const decoder = new TextDecoder("utf-8");
const text = decoder.decode(await Deno.readFile("file.txt"));

console.log(text);

Remove files in Deno (remove and removeSync)

To remove files in Deno, use either the remove or removeSync method.

// Deno remove file asynchronous (non-block)
await Deno.remove("file.txt");

// Deno remove file synchronous (blocking way)
Deno.removeSync("image.png");

If you try to delete a file that doesn’t exist, Deno throws an error.

Uncaught NotFound: No such file or directory (os error 2)

Check for a folder in your directory (ensureDir)

The ensureDir method ensures that a folder exists in your working directory. You can use it to write a simple program.


Over 200k developers use LogRocket to create better digital experiences

👁 Image
Learn more →

Let’s write a simple program that checks if a folder exist. If the folder exists, it creates a new file and adds some text content to it.

To use this method, you must import it into your application. Create a notes folder inside your working directory. This is where you’ll store your note.

import { ensureDir, ensureDirSync } from "https://deno.land/std/fs/mod.ts";

Like the other methods we’ve discussed so far, this one comes in both asynchronous and synchronous format. Here’s how to use the asynchronous method:

ensureDir("./notes")
 .then(() => Deno.writeTextFile("./notes/note1.txt", "This is the content for note 1")); 

Copy file contents (copy)

The copy method enables you to duplicate file contents to another file.

To import the copy method into your Deno application:

import { copy } from "https://deno.land/std/fs/mod.ts";
copy("file.txt", "test.txt", {
 overwrite: true,
});

This method copies the file contents from file.txt to test.txt. It also takes some options. The overwrite option, for example, overwrites the content of the file to which you are copying the content.

Check for a directory (exists)

The exists method checks if a directory exists. exists returns a promise while existsSync returns a boolean.

Let’s write a simple program that will check if a directory exists or not:

import { exists, existsSync } from "https://deno.land/std/fs/mod.ts";

exists("./notes").then((res) => console.log(res)); //res here returns a boolean
 //or do this
let fileExists = existsSync("./notes"); 
console.log(fileExists);// returns boolean

Check that a file exists (ensureFile)

ensureFile ensures that a file exists.

Let’s write a simple program to experiment with it:

import { ensureFile, ensureFileSync } from "https://deno.land/std/fs/mod.ts";
let ok = ensureFileSync("./read.ts");

If the file does not exist, Deno automatically creates it. Again, remember to add the --unstable flag since some of these methods are still experimental.


More great articles from LogRocket:


How to empty a directory in Deno (emptyDir)

The emptyDir method checks if a directory is empty. If the specified directory contains any file or directory, Deno empties the directory.

import { emptyDir } from "https://deno.land/std/fs/mod.ts";

emptyDir("./notes").then((res) => console.log("res", res)); // this method return a promise.

Creating a CLI application in Deno

Now let’s create a simple CLI application that will create files for us. We’ll use the terminal to create this file.

Deno.args returns all the arguments passed in a command. With this, we can create a simple application. Our command will hold the name of the file we want to create.

Run the following:

deno run main.ts create-file test.ts.

This command has two arguments: create-file and test.ts. If we log Deno.args, it will return the arguments in an array.

We can use this to check whether the u

ser passed the create-file param and provided a name for a file:

let params = Deno.args;
console.log(params);
let createFile = () => {
 if (params[0] !== "create-file") {
 console.log(
 `${params[0]} is not a valid command, Did you mean 'create-file'`,
 );
 return false;
 } else if (!params[1]) {
 console.log(
 `You need to provide a name of a file`,
 );
 return false;
 } else {
 Deno.writeTextFileSync(`./${params[1]}`, "//This is your created file");
 }
};
createFile();

Now to run the application, open up your terminal and run the following:

deno run --allow-all main.ts create-file <name of file>

Remember to add the --allow-all or --allow-write file flag.

Conclusion

As you can see, Deno’s file system is quite versatile, and there are multiple approaches and scenarios to consider when writing to a file to Deno. It’s always good to know how to use the synchronous and asynchronous methods as both have their use cases. When writing to larger files, you should consider taking advantage of the chunked writing capabilities of writing streams.

The source code used in this demo is available on GitHub.

👁 Image
👁 Image
👁 Image

Stop guessing about your digital experience with LogRocket

Get started for free

Recent posts:

How to build a virtual engineering team with Gemini CLI subagents

Learn how to use Gemini CLI subagents to delegate frontend, backend, testing, and docs tasks to specialized agents with guardrails and clear ownership.

👁 Image
Emmanuel John
Jun 18, 2026 ⋅ 10 min read

Debug Next.js apps with AI agents and next-browser

Learn how next-browser gives AI agents runtime context for debugging Next.js apps, including React props, hydration, PPR, forms, and performance.

👁 Image
Emmanuel John
Jun 17, 2026 ⋅ 9 min read

Stop hardcoding LLM SDKs: Dynamic LLM routing with OpenRouter and Next.js

Build dynamic LLM routing in Next.js with OpenRouter, TanStack AI, task classification, model fallbacks, and cost-aware routing.

👁 Image
Chizaram Ken
Jun 16, 2026 ⋅ 13 min read

What is TSRX?: What JSX would look like if it were designed today

TSRX adds first-class control flow, conditional hooks, and scoped styles to React via a TypeScript compiler extension — no new framework required.

👁 Image
Ikeh Akinyemi
Jun 12, 2026 ⋅ 6 min read
View all posts

Hey there, want to help make our blog better?

Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.

Sign up now