DEV Community

Santosh Shelar
Santosh Shelar

Posted on

1

10 Hidden Node.js Features You Probably Aren’t Using (but Should)

10 Hidden Node.js Features You Probably Aren’t Using (but Should)

Think you know Node.js inside out? These underrated features can improve your code quality, debugging experience, and app performance. Whether you're a backend pro or just diving into Node, these tips will come in handy.

Let’s dive in 👇


1. 🔍 --inspect Debugging Built-In

No need for external tools!

You can debug directly in Chrome:

node --inspect yourApp.js
Enter fullscreen mode Exit fullscreen mode

Then open chrome://inspect in Chrome.

Set breakpoints, step through code, inspect variables—all within the browser.


2. 🔄 util.promisify()

Tired of callbacks?

Convert any callback-based function into a Promise-based one:

const { promisify } = require('util');
const readFile = promisify(fs.readFile);
Enter fullscreen mode Exit fullscreen mode

Now you can use async/await with native Node APIs!


3. ⚠️ --trace-warnings

Don’t ignore warnings—trace them!

node --trace-warnings app.js
Enter fullscreen mode Exit fullscreen mode

You’ll see stack traces for runtime and deprecation warnings—super helpful for debugging and future-proofing your code.


4. ⏱️ process.hrtime() for Precise Timing

Need microsecond timing for benchmarking?

const start = process.hrtime();
// do something
const diff = process.hrtime(start);
console.log(`Execution time: ${diff[0]}s ${diff[1] / 1e6}ms`);
Enter fullscreen mode Exit fullscreen mode

More accurate than Date.now().


5. 🧵 Built-in worker_threads for Multi-threading

Yes, Node can do multi-threading natively!

Use worker_threads for CPU-heavy tasks like image processing, compression, etc., without blocking the main thread.

const { Worker } = require('worker_threads');
Enter fullscreen mode Exit fullscreen mode

Massively boosts performance when used wisely.


6. 🌐 globalThis Standard Global Object

Forget global or window—use:

globalThis
Enter fullscreen mode Exit fullscreen mode

It’s standard across all JavaScript environments: browser, Node, Deno.

Great for cross-platform or universal apps.


7. ✋ AbortController in Node.js

Cancel async tasks with ease!

const controller = new AbortController();
setTimeout(() => controller.abort(), 1000);
await fetch(url, { signal: controller.signal });
Enter fullscreen mode Exit fullscreen mode

Available since Node 15+. Works with fetch, streams, etc.


8. 🎯 Node’s Built-in events.once()

Only need to listen to an event once?

const { once } = require('events');
await once(emitter, 'data');
Enter fullscreen mode Exit fullscreen mode

Cleaner and safer than manually managing listeners.


9. 💬 readline.promises

Node 17+ introduced a Promise-based readline API:

const rl = readline.createInterface({ input, output });
const answer = await rl.question('What’s your name? ');
Enter fullscreen mode Exit fullscreen mode

Much neater than callback hell.


10. ⚙️ Environment-based NODE_OPTIONS

Tweak memory limits, enable flags, and more—without editing code:

NODE_OPTIONS="--max-old-space-size=4096" node app.js
Enter fullscreen mode Exit fullscreen mode

Useful for setting options in Docker containers, CI pipelines, etc.


🔥 Wrapping Up

Node.js is full of powerful features that often go unnoticed. If you found this post helpful, feel free to:

  • 🧵 Share it on Twitter
  • 🔖 Bookmark it for later
  • 💬 Comment with your favorite hidden Node gem!

🔗 Connect With Me


Happy coding! 💻

Follow me @learn_with_san for more insights on coding, and building in public. 🚀

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

Top comments (0)

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay