🧵 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
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);
Now you can use async/await
with native Node APIs!
3. ⚠️ --trace-warnings
Don’t ignore warnings—trace them!
node --trace-warnings app.js
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`);
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');
Massively boosts performance when used wisely.
6. 🌐 globalThis
Standard Global Object
Forget global
or window
—use:
globalThis
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 });
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');
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? ');
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
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!
Happy coding! 💻
0 Comments