DEV Community

Cover image for 5 NodeJS Features You Probably Missed
Tech Vision
Tech Vision

Posted on

5 NodeJS Features You Probably Missed

Are you wasting time installing unnecessary libraries and debugging your NodeJS applications the wrong way? Here are five built-in NodeJS features that can make your life easier and reduce your project's dependencies.

If you prefer the video version, here is the link 😉

1. Dotenv Replacement

Typically, we use the dotenv library to manage environment variables. However, NodeJS now offers native support for this.

Using --env-file Option:

node --env-file=.env app.js
Enter fullscreen mode Exit fullscreen mode

Using process.loadEnvFile (NodeJS v21+):

// server.js
process.loadEnvFile('.env');
Enter fullscreen mode Exit fullscreen mode

These methods eliminate the need for the dotenv library, streamlining your development process.

2. Nodemon Replacement

Instead of installing nodemon to automatically restart your app on changes, use NodeJS's built-in watch mode.

Run with --watch Option:

node --watch app.js
Enter fullscreen mode Exit fullscreen mode

This built-in feature provides the same functionality as nodemon without additional dependencies.

3. Built-in Test Runner

Writing tests can be tedious, especially for side projects. NodeJS now includes a built-in test runner, removing the need for external libraries.

Example Test File:

import { test, assert } from 'node:test';

test('simple test', (t) => {
  assert.strictEqual(1 + 1, 2);
});
Enter fullscreen mode Exit fullscreen mode

Run Tests:

node --test
Enter fullscreen mode Exit fullscreen mode

No more excuses for skipping tests!

4. UUID Generation

Generating unique values is common in many projects. Instead of using the uuid package, leverage NodeJS's crypto module.

Generate UUID:

import { randomUUID } from 'crypto';

const id = randomUUID();
console.log(id);
Enter fullscreen mode Exit fullscreen mode

5. Built-in Debugger

Many developers still use console.log for debugging, which is inefficient. NodeJS offers a powerful built-in debugger.

Run in Inspect Mode:

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

Using Chrome DevTools:

  1. Open Chrome and go to DevTools.
  2. Click the NodeJS icon.
  3. Use breakpoints and inspect objects efficiently.

Conclusion

Happy Coding 👋!
Thank you for reading the entire article; hope that's helpful to you.

Top comments (0)