Next

Core Modules & File System

  • Node.js core modules provide essential functionality for backend development. This content explains the fs module, demonstrates reading and writing files, and covers basic file operations for managing data efficiently in Node.js applications.
  • 🔹 Core Modules in Node.js

    Node.js comes with a set of built-in libraries called Core Modules. These modules allow developers to perform common tasks without installing extra packages. Core modules handle operations like file manipulation, networking, events, and streams.

    Some common core modules are:

    • fs – File System

    • http – Creating web servers

    • path – Handling file paths

    • os – Getting operating system info

    • events – Event handling

    • url – Working with URLs

    These modules can be imported directly using require().

    🔹 File System (fs Module)

    The fs module is used to work with files in Node.js. It provides methods to read, write, update, delete, and manipulate files.

    There are two types of methods:

    1. Synchronous – Blocking, waits for the operation to complete

    2. Asynchronous – Non-blocking, uses callbacks or promises

    🔹 Reading Files

    Explanation:

    • readFile reads a file asynchronously. The program continues while the file is being read.

    • readFileSync blocks the program until the file is read.

Asynchronous File Reading in Node.js

This code demonstrates reading a file asynchronously using Node.js fs module. The fs.readFile() method reads example.txt in the background, allowing other code (like the final console.log) to run without waiting. The file content is logged once reading completes.

const fs = require('fs');

fs.readFile('example.txt', 'utf8', (err, data) => {
    if (err) throw err;
    console.log('File content:', data);
});

console.log('Reading file in the background...');

Synchronous File Reading in Node.js

This code demonstrates reading a file synchronously using Node.js fs module. The fs.readFileSync() method blocks further execution until example.txt is fully read, then logs the file content. Execution pauses until reading is complete, unlike asynchronous reading.

const fs = require('fs');

const data = fs.readFileSync('example.txt', 'utf8');
console.log('File content:', data);
console.log('File read completed');
  • 🔹 Writing Files

    Explanation:

    • writeFile creates or replaces a file asynchronously.

    • writeFileSync does the same synchronously.

Asynchronous File Writing in Node.js

This code demonstrates writing data to a file asynchronously using Node.js fs module. The fs.writeFile() method writes 'Hello Node.js!' to output.txt in the background, and the callback confirms success once writing is complete without blocking other code execution.

const fs = require('fs');

fs.writeFile('output.txt', 'Hello Node.js!', (err) => {
    if (err) throw err;
    console.log('File written successfully!');
});

Synchronous File Writing in Node.js

This code demonstrates writing data to a file synchronously using Node.js fs module. The fs.writeFileSync() method writes 'Hello Node.js!' to output.txt and blocks further execution until the write is complete, ensuring the file is fully written before moving on.

const fs = require('fs');

fs.writeFileSync('output.txt', 'Hello Node.js!');
console.log('File written successfully (sync)!');
  • 🔹 Append to File

    Adds content without overwriting existing data:

Asynchronous File Append in Node.js

This code demonstrates appending data to a file asynchronously using Node.js fs module. The fs.appendFile() method adds '\nThis is new content' to output.txt without overwriting existing content. The callback confirms once the operation is complete, allowing other code to run concurrently.

const fs = require('fs');

fs.appendFile('output.txt', '\nThis is new content', (err) => {
    if (err) throw err;
    console.log('Content appended!');
});
  • File Operations Basics

    1. Create File – writeFile or open

    2. Read File – readFile or readFileSync

    3. Update File – appendFile

    4. Delete File – unlink

    Explanation:

    • Node.js delegates file operations to the background thread.

    • Once complete, the callback is sent to the event loop and executed in the call stack.

    ⚡ Key Points

    • fs is asynchronous by default. Synchronous methods are blocking.

    • Use asynchronous methods for server-side operations to avoid freezing the app.

    • All core modules like fs, path, os are built-in, no installation required.

    • Append to File

    This method adds content without overwriting the existing file.

Next