File Operations in Node.js

From the Node.js Core Modules cheat sheet ยท File System (fs) ยท verified Jul 2026

File Operations

Read, write, delete files with async and sync methods

javascript
// Read file
const data = fs.readFileSync('file.txt', 'utf8');
fs.readFile('file.txt', 'utf8', (err, data) => {});

// Write file
fs.writeFileSync('file.txt', 'content');
fs.appendFileSync('file.txt', 'more');

// Delete/Check
fs.unlinkSync('file.txt');
fs.existsSync('file.txt');
๐ŸŸข Essential - Every Node.js app deals with files
๐Ÿ’ก Use async methods (fs.promises) to avoid blocking
โš ๏ธ Always handle errors in file operations
๐Ÿ“Œ Use path.join() for cross-platform paths
โšก Stream large files instead of reading all at once
๐Ÿ”— Related: fs-extra for additional utilities

More Node.js tasks

Back to the full Node.js Core Modules cheat sheet