Directory Operations in Node.js

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

Directory Operations

Create, read, and remove directories

javascript
const fs = require('fs').promises
const path = require('path')

// Create directory (recursive)
await fs.mkdir('path/to/dir', { recursive: true })

// Read directory contents
const files = await fs.readdir('./src')
// With file types
const entries = await fs.readdir('./src', { withFileTypes: true })
const dirs = entries.filter(e => e.isDirectory())

// Remove directory
await fs.rmdir('path/to/dir')
// Remove with contents (Node 14+)
await fs.rm('path/to/dir', { recursive: true, force: true })
💡 Use recursive: true to create nested directories
⚡ readdir with withFileTypes avoids extra stat calls
⚠️ fs.rm() with force: true is like rm -rf (dangerous)
📌 Watch has platform-specific limitations
🔗 Related: glob for pattern matching
filesystemdirectories

Continue with Node.js Core Modules

Save the full cheat sheet or work through every related task.

More Node.js tasks

Back to the full Node.js Core Modules cheat sheet