File System — Complete Guide
File System — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Node.js Tutorial on Toolliyo Academy.
On this page
File System
This lesson covers File System. If this feels new, that is normal. We will build up slowly.
What you will learn
- What file system means — in normal words, not textbook words
- How it works step by step
- Code you can run today on your laptop
- Where teams use this in real projects
Before you start
- Software: Node.js LTS from nodejs.org, VS Code, and a terminal
- Knowledge: Lessons 1–6 in this course
- Previous lesson: Modules — Complete Guide
Explain it simply
The fs module reads and writes files on disk. Always prefer the async versions so you do not freeze the server.
Why developers use this
- Read config and upload files
- Write logs and exports
- Async fs keeps other requests moving
How it works (step by step)
- You write JavaScript in a
.jsfile about File System. - You run it with
node filename.jsin the terminal. - Node prints output or starts a server depending on the lesson.
- You change one line, run again, and see what changed — that is how you learn.
Code example — type this yourself
const fs = require('fs/promises');
async function main() {
await fs.writeFile('note.txt', 'Hello file!');
const text = await fs.readFile('note.txt', 'utf8');
console.log(text);
}
main();
fs/promises returns Promises you can await. Avoid readFileSync in web servers.
What each part does
const fs = require('fs/promises');— Loads a built-in module or package you installed with npm.async function main() {— Async work — Node can serve other users while this waits.await fs.writeFile('note.txt', 'Hello file!');— Async work — Node can serve other users while this waits.const text = await fs.readFile('note.txt', 'utf8');— Async work — Node can serve other users while this waits.console.log(text);— Prints to the terminal — great for learning; use proper logging in production.}— Line 6: runs as written.main();— Line 7: runs as written.
Real life: where File System shows up
A startup team uses File System when they bootstrap their first API. The developer runs a small script on a laptop, stores config in .env, and splits code into modules before the app grows. Start small: one feature working beats a perfect architecture on paper.
Try it yourself — hands-on
- Create fs-demo.js with the code
- Run node fs-demo.js
- Check that note.txt was created
Common mistakes (avoid these)
- Using readFileSync inside an Express route — it blocks every other user.
Interview note
Interviewers often ask: “What is File System?” Answer in one sentence, then give a tiny example you actually ran.
Summary
- Use fs/promises with async/await
- Specify utf8 for text files
- Do not use Sync methods on request paths
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!