Node.js is packed with powerful tools, but many developers only scratch the surface. While we often use features like fs
for file handling or http
for APIs, there are hidden gems in Node.js that can make coding easier and faster.
In this post, Iβll show you five lesser-known but super useful Node.js features - with real examples you can use today.
1. The path
Module for Cleaner File Paths
If youβve ever worked with file paths, you know how frustrating it can be to handle slashes (/ vs ) on different operating systems. Instead of manually stitching paths together, Node.js gives you the path
module to do it the right way.
Example:
import { join, resolve } from 'path';
const filePath = join(__dirname, 'files', 'data.txt');
console.log(filePath); // Works on Windows, macOS, and Linux
const absolutePath = resolve('files', 'data.txt');
console.log(absolutePath); // Returns the absolute path
Why use this?
- β Avoids OS-specific path issues
- β Makes file handling cleaner and safer
- β Works perfectly with file uploads, logs, and config files
2. AbortController
to Cancel Fetch Requests
When making API calls, sometimes we donβt need to wait for a response. For example, if a user types in a search bar, the previous request should cancel when they type a new query.
Node.js has AbortController
, which lets you cancel API requests on demand.
Example:
const controller = new AbortController();
const signal = controller.signal;
fetch('https://jsonplaceholder.typicode.com/todos/1', { signal })
.then(res => res.json())
.then(data => console.log(data))
.catch(err => console.error('Request aborted:', err));
setTimeout(() => controller.abort(), 100); // Cancels request after 100ms
Why use this?
- β Saves bandwidth and resources
- β Stops unnecessary API calls
- β Works great for search bars, live filters, and pagination
3. worker_threads
for Multi-Threading
By default, Node.js runs on a single thread, which means heavy tasks (like processing images or parsing large files) can slow down your app. But worker_threads
lets you run code in parallel, keeping your app fast.
Example:
import { Worker } from 'worker_threads';
const worker = new Worker('./worker.js'); // Runs worker.js in a new thread
worker.on('message', (msg) => console.log('Worker says:', msg));
worker.on('error', (err) => console.error('Worker error:', err));
worker.on('exit', () => console.log('Worker stopped'));
Inside worker.js
(a separate file):
import { parentPort } from 'worker_threads';
parentPort.postMessage('Hello from Worker!'); // Sends message to main thread
Why use this?
- β Keeps your main app fast and responsive
- β Helps with CPU-intensive tasks like image processing
- β Perfect for background jobs and real-time processing
4. dns.promises
for Fast DNS Lookups
Most people use third-party libraries to resolve domain names, but Node.js has a built-in way to do this - without extra dependencies.
Example:
import { resolve } from 'dns/promises';
async function getIP(domain) {
try {
const addresses = await resolve(domain);
console.log(`${domain} resolves to:`, addresses);
} catch (err) {
console.error('Error resolving domain:', err);
}
}
getIP('google.com'); // Example output: ['142.250.182.206']
Why use this?
- β Avoids extra dependencies
- β Useful for server-side networking tasks
- β Works great for custom DNS resolvers
5. The Built-in timers/promises
for Async Delays
We all use setTimeout()
, but when you need delays inside async/await
functions, things get messy.
Instead of using workarounds, Node.js gives us timers/promises
to make it clean and readable.
Example:
import { setTimeout } from 'timers/promises';
async function delayTask() {
console.log('Starting...');
await setTimeout(2000); // Waits for 2 seconds
console.log('Done after 2 seconds!');
}
delayTask();
Why use this?
- β Makes async delays easier to read
- β Avoids the need for callbacks or extra logic
- β Works great for rate limiting, retries, and animations
Final Thoughts
Most developers overlook these Node.js features, but they can save you time and effort when building apps.
- β path for cleaner file handling
- β AbortController for canceling API calls
- β worker_threads for multi-threading
- β dns.promises for native DNS lookups
- β timers/promises for async delays
Try them out in your next project and see the difference! Do you have any favorite hidden Node.js features? Drop a comment below! π
Tags: #NodeJS #WebDevelopment #CodingTips #JavaScript #Backend
Comments
Please login to publish your comment!
By logging in, you agree to our Terms of Service and Privacy Policy.
No comments here!