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
Using process.loadEnvFile (NodeJS v21+):
// server.js
process.loadEnvFile('.env');
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
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);
});
Run Tests:
node --test
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);
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
Using Chrome DevTools:
- Open Chrome and go to DevTools.
- Click the NodeJS icon.
- Use breakpoints and inspect objects efficiently.
Conclusion
Happy Coding 👋!
Thank you for reading the entire article; hope that's helpful to you.
Top comments (2)
This is perfect collection of the awesome tips for a beginner or a professional! Thanks @techvision 😄
Thank you @prakirth