DEV Community

Ayako yk
Ayako yk

Posted on

Exploring File System Basics in Node.js

Node.js provides extensive features for working with filesystems. Covering everything at once would be overwhelming, so in this article, I'll focus on the basics. Advanced topics will be explored in the next blog.

  1. What is a File System?
  2. Working with Files
  3. Basic Operations
  4. Path Modules

What is a File System?
A file system (or filesystem) is a logical structure that organizes, manages, and provides access to files and directories on storage devices like hard drives or SSDs. Filesystems can vary between operating systems, but some are designed to work across multiple platforms or for specific applications.

Working with Files
Source: Node.js documentation

File Descriptors
A file descriptor is a numerical identifier for an open file. Node.js provides the fs module to handle file descriptors:

const fs = require("node:fs"); fs.open('/user/example.txt', 'r', (err, fd) => { if (err) throw err; console.log(`File descriptor: ${fd}`); }) 
Enter fullscreen mode Exit fullscreen mode

For synchronous operations, you can use openSync method to get a file descriptor without using a callback:

const fd = fs.openSync('/user/exampl.txt', 'r'); console.log(`File descriptor: ${fd}`); 
Enter fullscreen mode Exit fullscreen mode

The 'r' in the above examples is a file open flag, which specifies how the file is accessed. Refer to the documentation for additional flags.

Basic Operations
Source: Node.js documentation

Reading Files
Node.js offers three primary ways to read files:

readFile()

const fs = require('node:fs'); fs.readFile('/user/example.txt', 'utf-8, (err, data) => { if (err) { console.log(err); return; } else { console.log(data); ); 
Enter fullscreen mode Exit fullscreen mode

readFileSync()

const fs = require('node:fs'); const data = fs.readFileSync('/user/example.txt', 'utf-8'); console.log(data); 
Enter fullscreen mode Exit fullscreen mode

Promise-based readFile()

const fs = require('node:fs/promises'); async function example() { try { const data = await fs.readFile('/user/example.txt', { encoding: 'utf-8' }); consle.log(data); } catch (err) { console.log(err); } } example(); 
Enter fullscreen mode Exit fullscreen mode

Writing Files
Similarly, there are three ways to write files:

writeFile()

const fs = require('node:fs'); const content = "Some content."; fs.writeFile('/user.example.txt', content, (err) => { if (err) { console.log(err); } else { // Handle success } }); 
Enter fullscreen mode Exit fullscreen mode

writeFileSync()

const fs = require('node:fs'); const content = "Some content."; try { fs.writeFileSync('/user/example.txt', content); } catch (err) { console.log(err); } 
Enter fullscreen mode Exit fullscreen mode

Promise-based writeFile()

const fs = require('node:fs/promises'); async function example() { try { const content = "Some content."; await fs.writeFile('/user/example.txt', content); } catch (err) { console.log(err); } } example(); 
Enter fullscreen mode Exit fullscreen mode

Appending Files
The appendFile methods are useful when you want to add content to the end of an existing file without truncating or overwriting its current content.

appendFile()

const fs = require('node:fs'); const content = 'Some content.'; fs.appendFile('/user/example.txt', content, (err) => { if (err) { console.log(err); } else { // Handle success } } 
Enter fullscreen mode Exit fullscreen mode

asppendFileSync()

const fs = require('node:fs'); const content = 'Some content.'; try { fs.appendFileSync('/user/example.txt', content); } catch (err) { console.log(err); } 
Enter fullscreen mode Exit fullscreen mode

Promise-based appendFile()

const fs = require('node:fs/promises'); const content = 'Some content'; async function example() { try { await fs.appendFile('/user/example.txt', content); } catch (err) { console.log(err); } } example(); 
Enter fullscreen mode Exit fullscreen mode

Path Modules
The path module provides utilities for working with file paths. Common methods include:

path.join()
Joins path segments into a single path string:

const path = require("node:path"); const name = 'jane'; const result = path.join('/', 'user', name, 'notes.txt'); console.log(result); // "/user/jane/notes.txt" 
Enter fullscreen mode Exit fullscreen mode

path.resolve()
Returns an absolute path by resolving segments relative to the current working directory:

const absolutePath = path.resolve('joe.txt'); console.log(absolutePath); // "/user/joe/joe.txt" 
Enter fullscreen mode Exit fullscreen mode

These are some of the foundational features Node.js offers for working with filesystems. To deepen your understanding, explore the Node.js documentation, which provides detailed explanations and additional examples. In the next article, I'll dive into best practices for handling complex filesystem operations.

Top comments (0)