File System Operations with Node.js

Node.js, with its non-blocking I/O and robust libraries, provides an excellent environment for performing file system operations. Whether you're reading files, writing data, or manipulating directories, Node.js simplifies working with the file system. In this blog post, we'll explore the essential file system operations in Node.js, complete with a handy cheatsheet to reference whenever you need it.

File System Basics in Node.js

Node.js provides a built-in fs module that allows you to interact with the file system. To get started, you need to require the module:

const fs = require("fs");

1. Reading Files

Synchronous File Reading

const data = fs.readFileSync("file.txt", "utf8");
console.log(data);

Asynchronous File Reading

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

2. Writing Files

Synchronous File Writing

fs.writeFileSync("newfile.txt", "Hello, Node.js!", "utf8");

Asynchronous File Writing

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

3. Checking if a File Exists

fs.access("file.txt", fs.constants.F_OK, (err) => {
  if (err) {
    console.error("File does not exist.");
    return;
  }
  console.log("File exists.");
});

4. Renaming and Moving Files

fs.rename("oldfile.txt", "newfile.txt", (err) => {
  if (err) throw err;
  console.log("File renamed successfully.");
});

5. Deleting Files

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

6. Working with Directories

Creating a Directory

fs.mkdir("myfolder", (err) => {
  if (err) throw err;
  console.log("Directory created successfully.");
});

Removing a Directory

fs.rmdir("myfolder", (err) => {
  if (err) throw err;
  console.log("Directory deleted successfully.");
});

7. Listing Files in a Directory

fs.readdir("myfolder", (err, files) => {
  if (err) throw err;
  console.log("Files in directory:", files);
});

File System Cheatsheet

Here's a quick reference cheatsheet for common file system operations in Node.js:

Reading Files:

  • Synchronous: fs.readFileSync(path, encoding)
  • Asynchronous: fs.readFile(path, encoding, callback)

Writing Files:

  • Synchronous: fs.writeFileSync(path, data, encoding)
  • Asynchronous: fs.writeFile(path, data, encoding, callback)

Checking File Existence:

  • fs.access(path, fs.constants.F_OK, callback)

Renaming and Moving Files:

  • fs.rename(oldPath, newPath, callback)

Deleting Files:

  • fs.unlink(path, callback)

Working with Directories:

  • Creating a Directory: fs.mkdir(path, callback)
  • Removing a Directory: fs.rmdir(path, callback)
  • Listing Files in a Directory: fs.readdir(path, callback)

Conclusion

Node.js's fs module provides a wide range of file system operations, making it a powerful tool for handling files and directories in your applications. With this guide and cheatsheet, you now have a solid understanding of the basics of file system operations in Node.js. Whether you're building a file uploader, a file manager, or simply reading and writing data, Node.js makes it straightforward and efficient.