Node.js logoNode.js INTERMEDIATE

Node.js Core Modules

Essential Node.js built-in modules including File System, HTTP, Process, OS, Crypto, Streams, and Events

15 min read
nodejsfshttpcryptostreamseventsprocessos

File System (fs)

Read, write, and manipulate files and directories

File Operations

Read, write, delete files with async and sync methods

javascript
🟢 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

Path Module

Work with file and directory paths across platforms

javascript
🟢 Essential - Handle paths correctly on all OS
💡 Always use path.join() not string concatenation
📌 __dirname is current file's directory
⚡ path.resolve() creates absolute paths
⚠️ Windows uses \ while Unix uses /
🔗 Related: process.cwd() for working directory

Directory Operations

Create, read, and remove directories

javascript
💡 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

HTTP & HTTPS

Create servers and make HTTP requests

HTTP Server

Create HTTP servers to handle web requests

javascript
🟢 Essential - Foundation of web servers in Node.js
💡 Use Express.js for production applications
📌 Server emits events: request, connection, close
⚡ Use cluster module for multi-core utilization
⚠️ Handle server errors to prevent crashes
🔗 Related: https module for SSL/TLS

HTTP Client Requests

Make HTTP requests to external APIs and services

javascript
💡 Consider using axios or node-fetch for easier API
📌 Response is a readable stream
⚡ Set timeout to prevent hanging requests
⚠️ Always handle network errors
🟢 Essential for API integration
🔗 Related: https.request() for secure requests

Process & OS

Interact with system processes and operating system

Process Module

Access command line arguments, environment, and control process

javascript
🟢 Essential - Control Node.js runtime behavior
💡 process.env for environment variables
📌 process.argv contains command line arguments
⚠️ process.exit() immediately terminates (use carefully)
⚡ process.nextTick() for async scheduling
🔗 Related: dotenv for .env file loading

OS Module

Get operating system information and utilities

javascript
💡 Useful for system monitoring and diagnostics
📌 os.platform() returns darwin, win32, linux
⚡ os.cpus() for CPU info and core count
🟢 Essential for cross-platform apps
🔗 Related: systeminformation for detailed stats

Child Process

Execute system commands and spawn new processes

javascript
💡 exec for simple commands, spawn for complex/streaming
⚠️ Sanitize user input to prevent command injection
📌 fork() is specifically for Node.js scripts
⚡ Use worker_threads for CPU-intensive tasks instead
🔗 Related: execa for better child process handling
processsystem

Crypto & Buffer

Cryptography operations and binary data handling

Crypto Module

Cryptographic functionality for hashing, encryption, and security

javascript
🟢 Essential - Security and data integrity
💡 Use crypto.randomBytes() for secure random values
⚠️ Never use Math.random() for security
📌 bcrypt is better for password hashing
⚡ Use streaming API for large files
🔗 Related: jsonwebtoken for JWT tokens

Buffer Module

Handle binary data and convert between encodings

javascript
💡 Buffers are fixed-size byte arrays
📌 Use Buffer.from() not new Buffer() (deprecated)
⚡ Buffers are more memory efficient than strings
⚠️ Be careful with buffer overflows
🔗 Related: stream.Buffer for streaming operations

Streams & Events

Handle streaming data and event-driven programming

Streams

Process data piece by piece without loading all into memory

javascript
🟢 Essential - Handle large files and real-time data
💡 Four types: Readable, Writable, Duplex, Transform
⚡ Streams save memory and improve performance
📌 pipe() handles backpressure automatically
⚠️ Always handle stream errors
🔗 Related: stream/promises for async iteration

Event Emitter

Create and handle custom events for async communication

javascript
🟢 Essential - Core of Node.js event-driven architecture
💡 Many Node.js objects extend EventEmitter
📌 removeListener() to prevent memory leaks
⚠️ Default max 10 listeners (setMaxListeners to change)
⚡ once() for one-time event handlers
🔗 Related: events.EventEmitter is the base class

Utilities & Advanced

Utility functions and advanced Node.js features

URL & QueryString

Parse and construct URLs and query strings

javascript
🟢 Essential - URL parsing for web applications
💡 Use new URL() instead of legacy url.parse()
📌 URLSearchParams for easy query manipulation
⚡ URL validation happens in constructor
🔗 Related: path module for file paths
urlutilities

Util Module

Utility functions for debugging and promisification

javascript
💡 promisify() converts callbacks to promises
📌 util.inspect() is great for debugging
⚡ TextEncoder/Decoder for UTF-8 conversion
🟢 Essential for modernizing callback code
🔗 Related: console.dir() for simple inspection
utilitiesdebugging

Worker Threads

Run JavaScript in parallel threads for CPU-intensive tasks

javascript
🔴 Advanced - For CPU-intensive parallel processing
💡 Workers run in separate V8 isolates
⚠️ No shared memory (use SharedArrayBuffer carefully)
⚡ Better than child_process for JavaScript code
📌 Not for I/O operations (use async/await instead)
🔗 Related: cluster for network server scaling
parallelperformance

Cluster Module

Scale Node.js apps across multiple CPU cores

javascript
🟢 Essential for production Node.js servers
💡 Workers share server ports automatically
⚡ Scales to all CPU cores for better performance
📌 Master process manages workers
⚠️ Workers die independently, implement restart logic
🔗 Related: pm2 for production process management
scalingperformance