This content originally appeared on DEV Community and was authored by Ayako yk
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.
- What is a File System?
- Working with Files
- Basic Operations
- 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}`);
})
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}`);
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);
);
readFileSync()
const fs = require('node:fs');
const data = fs.readFileSync('/user/example.txt', 'utf-8');
console.log(data);
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();
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
}
});
writeFileSync()
const fs = require('node:fs');
const content = "Some content.";
try {
fs.writeFileSync('/user/example.txt', content);
} catch (err) {
console.log(err);
}
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();
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
}
}
asppendFileSync()
const fs = require('node:fs');
const content = 'Some content.';
try {
fs.appendFileSync('/user/example.txt', content);
} catch (err) {
console.log(err);
}
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();
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"
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"
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.
This content originally appeared on DEV Community and was authored by Ayako yk