KoderSolutionKoderSolution LogoKoderSolution Logo
HomeArticlesTutorialsToolsRun CodeForumAI Lab
🟢

Node.js

Topic Hub & Articles

Node.js Intro

10 min

Node.js Get Started

10 min

Node.js Modules

10 min

Node.js HTTP Module

10 min

Node.js File System

10 min

Node.js URL Module

10 min

Node.js NPM

10 min

Node.js Events

10 min

Node.js Upload Files

10 min

Node.js Email

10 min

Node.js Buffer

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js Streams

10 min

Node.js Crypto

10 min

Node.js OS Module

10 min

Node.js Path Module

10 min

Node.js Global Objects

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js Process

10 min

Node.js Child Processes

10 min

Node.js Worker Threads

10 min

Node.js DNS Module

10 min

Node.js Query String

10 min

Recap Quiz

Gated

Milestone Assessment

MySQL Connect

10 min

MySQL Create Database

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js MySQL ORDER BY Clause

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Intro

10 min

MongoDB Create Database

10 min

MongoDB Create Collection

10 min

MongoDB Insert

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Find

10 min

MongoDB Query

10 min

MongoDB Sort

10 min

MongoDB Delete

10 min

MongoDB Update

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Limit

10 min

MongoDB Join

10 min

Progress
0%

0 / 35 Lessons

KoderSolutionKoderSolution LogoKoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Facebook PageX (Twitter) ProfileYouTube ChannelContact Support

Engineering Newsletter

Subscribe for weekly software engineering tutorials, architecture guides, and developer tool updates.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
Node.jsNode.js Tutorial
Lesson

Node.js File System

10 min reading
Free Course

Node.js File System Module: Async File Operations with fs/promises

The built-in node:fs/promises module lets Node.js scripts read, write, append, list, and delete files on disk using clean async / await promises. Unlike legacy callback-based fs methods or blocking *Sync helpers, fs/promises keeps your server event loop responsive while disk operations run in the background.

Reading, Writing, and Appending Files

Pass 'utf8' encoding when reading text files so readFile() returns a JavaScript string instead of a raw binary Buffer. Use { recursive: true } with mkdir() so nested directories are created safely without throwing if they already exist.

import { mkdir, readFile, writeFile, appendFile } from 'node:fs/promises';
import path from 'node:path';

async function saveAndReadNote() {
  const dataDir = path.join(import.meta.dirname, 'data');
  const notePath = path.join(dataDir, 'notes.txt');

  await mkdir(dataDir, { recursive: true });
  await writeFile(notePath, 'First line saved.
', 'utf8');
  await appendFile(notePath, 'Second line appended.
', 'utf8');

  const content = await readFile(notePath, 'utf8');
  console.log(content);
}

saveAndReadNote().catch((err) => console.error('FS error:', err.message));

Handling Missing Files (ENOENT) Without Race Conditions

Avoid calling fs.existsSync() right before readFile(), because another process could delete or move the file between those two checks. Instead, call readFile() directly inside try / catch and inspect err.code === 'ENOENT'.

Common Mistakes to Avoid

  • Using fs.readFileSync() inside API request handlers: Synchronous disk methods block the entire Node.js thread until the OS finishes reading, stalling every other concurrent user. Reserve *Sync methods strictly for startup configs or CLI scripts.
  • Concatenating untrusted filenames with path.join() alone: path.join('/uploads', '../../.env') resolves outside /uploads. Verify resolvedPath.startsWith(safeBaseDir + path.sep) before touching disk.
  • Loading multi-gigabyte files with readFile(): readFile() buffers the whole file in memory. Switch to fs.createReadStream() for large files.

Frequently Asked Questions

What is the difference between node:fs and node:fs/promises?

node:fs exports callback and synchronous methods, whereas node:fs/promises exports promise-returning functions built specifically for async / await.

How do I delete a file safely in Node.js?

Use await fs.rm(filePath, { force: true }) so missing files do not throw ENOENT errors.

How do I list all files inside a directory?

Call await fs.readdir(dirPath, { withFileTypes: true }), which returns Dirent objects with .isFile() and .isDirectory() helpers.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground

Related Developer Tools & Topic Hub

Node.js Topic Hub
JSON Formatter
Validate, format, and minify JSON
Regex Tester
Test regular expression patterns
JWT Decoder
Inspect JSON Web Token payloads
All Node.js Articles
Deep-dive guides, tips & discussions
PreviousNode.js HTTP Module
Continue JourneyNext: Node.js URL Module

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum