Node.js fs Module - Full Tutorial

The fs (File System) module in Node.js provides an API for interacting with the file system. You can read, write, update, delete, and manage files and directories directly from your code.

1. fs.readFile()

Purpose: Asynchronously reads the entire contents of a file.

Syntax:

fs.readFile(path, options, callback)

Parameters:

Example:

const fs = require('fs');

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

Use Case: Reading configuration files, logs, templates.

2. fs.writeFile()

Purpose: Asynchronously writes data to a file, replacing the file if it already exists.

Syntax:

fs.writeFile(file, data, options, callback)

Parameters:

Example:

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

Use Case: Saving user input, logs, generated content.

3. fs.appendFile()

Purpose: Appends data to a file. If the file doesn't exist, it's created.

Syntax:

fs.appendFile(path, data, options, callback)

Example:

fs.appendFile('log.txt', 'New log entry\n', (err) => {
    if (err) throw err;
    console.log('Log updated!');
});

Use Case: Logging events, adding history entries.

4. fs.unlink()

Purpose: Deletes a file asynchronously.

Syntax:

fs.unlink(path, callback)

Example:

fs.unlink('output.txt', (err) => {
    if (err) throw err;
    console.log('File deleted successfully!');
});

Use Case: Cleaning up temporary or obsolete files.

5. fs.rename()

Purpose: Renames or moves a file.

Syntax:

fs.rename(oldPath, newPath, callback)

Example:

fs.rename('oldName.txt', 'newName.txt', (err) => {
    if (err) throw err;
    console.log('Rename successful!');
});

Use Case: File organization, processing workflows.

6. fs.mkdir()

Purpose: Creates a new directory.

Syntax:

fs.mkdir(path, options, callback)

Example:

fs.mkdir('newFolder', { recursive: true }, (err) => {
    if (err) throw err;
    console.log('Directory created!');
});

Use Case: Setting up storage directories dynamically.

7. fs.rmdir() (Deprecated in favor of fs.rm())

Purpose: Removes a directory.

Syntax:

fs.rmdir(path, callback)

Example:

fs.rmdir('newFolder', (err) => {
    if (err) throw err;
    console.log('Directory removed!');
});

Use Case: Cleaning up after tasks, removing empty folders.

8. fs.readdir()

Purpose: Reads the contents of a directory.

Syntax:

fs.readdir(path, options, callback)

Example:

fs.readdir('./', (err, files) => {
    if (err) throw err;
    console.log(files);
});

Use Case: Listing files for file managers, galleries, etc.

9. fs.stat()

Purpose: Retrieves the status/statistics of a file or directory.

Syntax:

fs.stat(path, callback)

Example:

fs.stat('example.txt', (err, stats) => {
    if (err) throw err;
    console.log(stats);
});

Use Case: Checking file size, creation date, modified date.

10. fs.copyFile()

Purpose: Copies a file from one location to another.

Syntax:

fs.copyFile(src, dest, mode, callback)

Example:

fs.copyFile('example.txt', 'example-copy.txt', (err) => {
    if (err) throw err;
    console.log('File copied successfully!');
});

Use Case: Backup operations, duplicating templates.