5 Lesser-Known Node.js Features That Can Save You Time

Published on 21st March, 2025

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!