Node.js File System

The Node.js fs (File System) module provides a powerful API to interact with the file system, allowing developers to read, write, and manipulate files and directories. The module supports both synchronous and asynchronous methods to handle file operations efficiently.

 

Key Features of File System

  1. Read/Write Files: You can use fs to read and write files, including text and binary files.
  2. Directory Manipulation: Allows you to create, delete, and manipulate directories.
  3. Asynchronous and Synchronous Methods: The fs module provides both asynchronous methods for non-blocking operations and synchronous methods for simpler workflows.
  4. File Metadata: You can retrieve file metadata, such as size, permissions, and timestamps.

 

Common File System Methods

1. fs.readFile()

The fs.readFile() method reads the contents of a file asynchronously.

const fs = require('fs');

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

Output:

Content of the example.txt file
  • The file is read asynchronously, and the callback function is invoked once the data is available.

2. fs.readFileSync()

The fs.readFileSync() method is a synchronous version of fs.readFile(). It blocks the execution until the file is completely read.

const fs = require('fs');

const data = fs.readFileSync('example.txt', 'utf8');
console.log(data);

Output:

Content of the example.txt file
  • Unlike readFile(), readFileSync() blocks the program execution until the file is fully read.

3. fs.writeFile()

The fs.writeFile() method is used to write data to a file asynchronously. If the file does not exist, it will be created.

const fs = require('fs');

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

Output:

File written successfully
  • The file content is written asynchronously, and a callback function is invoked once the operation completes.

4. fs.writeFileSync()

The fs.writeFileSync() method is a synchronous version of fs.writeFile().

const fs = require('fs');

fs.writeFileSync('output.txt', 'Hello, Node.js!');
console.log('File written successfully');

Output:

File written successfully
  • This method blocks the execution until the file is written completely.

5. fs.appendFile()

The fs.appendFile() method appends data to an existing file. If the file does not exist, it is created.

const fs = require('fs');

fs.appendFile('output.txt', '\nAppended Text', (err) => {
  if (err) throw err;
  console.log('Data appended');
});

Output:

Data appended
  • The method appends data to the end of the file asynchronously.

6. fs.unlink()

The fs.unlink() method deletes a file asynchronously.

const fs = require('fs');

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

Output:

File deleted
  • The file is deleted asynchronously.

7. fs.rename()

The fs.rename() method is used to rename or move a file asynchronously.

const fs = require('fs');

fs.rename('oldname.txt', 'newname.txt', (err) => {
  if (err) throw err;
  console.log('File renamed');
});

Output:

File renamed
  • This method is used to rename a file or move it to a different location.

8. fs.mkdir()

The fs.mkdir() method is used to create a new directory asynchronously.

const fs = require('fs');

fs.mkdir('new_directory', (err) => {
  if (err) throw err;
  console.log('Directory created');
});

Output:

Directory created
  • Creates a new directory asynchronously.

9. fs.readdir()

The fs.readdir() method reads the contents of a directory asynchronously.

const fs = require('fs');

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

Output:

[ 'file1.txt', 'file2.txt', 'new_directory' ]
  • Lists all the files and directories in the specified directory.

 

Summary

The fs module in Node.js provides an extensive set of methods for file and directory operations, such as reading, writing, renaming, deleting files, and creating directories. You can choose between asynchronous and synchronous methods depending on your application's needs. Commonly used methods include readFile(), writeFile(), appendFile(), and unlink(). Using these methods, Node.js enables efficient file handling, making it easy to build applications that work with local files.