When you start learning Node.js, one of the first things you’ll come across is npm (Node Package Manager). It lets you install useful packages (also called dependencies) that make coding easier.
But with thousands of npm packages available, which ones should you start with?
In this post, I’ll share 5 npm packages that are super useful for beginners, along with simple examples to show how they work.
Let’s get started! 🚀
1️⃣ nodemon – Automatically Restart Your Server
Every time you change your code in a Node.js app, you have to stop and restart the server manually. That gets annoying fast!
nodemon
solves this problem. It watches your files and automatically restarts the server whenever you make a change.
How to Install
npm install -g nodemon
How to Use
Instead of running your server with node
, use:
nodemon index.js
Now, whenever you update index.js
, nodemon will restart the server automatically. No more manual restarts!
2️⃣ dotenv – Manage Environment Variables
If your app uses sensitive data like API keys or database credentials, you should not put them directly in your code. Instead, you can store them in a .env
file and load them using dotenv
.
How to Install
npm install dotenv
How to Use
Create a .env
file in your project:
API_KEY=12345abcdef
Then, load it in your Node.js code:
require('dotenv').config();
console.log(process.env.API_KEY);
Now, your API key is safely stored outside your main code.
3️⃣ axios – Make HTTP Requests Easily
If your app needs to fetch data from an API, axios
makes it simple. It’s easier to use than the built-in fetch
function and works well with promises.
How to Install
npm install axios
How to Use
Here’s how you can make a simple GET request:
const axios = require('axios');
axios.get('https://jsonplaceholder.typicode.com/posts/1')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error('Error fetching data:', error);
});
This will fetch a sample blog post from a free API and log it in the console.
4️⃣ express – Build Web Servers Quickly
express
is one of the most popular frameworks for building web applications in Node.js. It makes it super easy to create APIs and handle requests.
How to Install
npm install express
How to Use
This simple Express server responds with "Hello, World!" when you visit http://localhost:3000
:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Now, your Node.js app has a working web server!
5️⃣ moment – Work with Dates Easily
JavaScript’s built-in date handling is a bit tricky. moment
makes it much easier to format, parse, and manipulate dates.
How to Install
npm install moment
How to Use
const moment = require('moment');
const now = moment();
console.log('Current Date:', now.format('YYYY-MM-DD HH:mm:ss'));
const futureDate = moment().add(7, 'days');
console.log('Date after 7 days:', futureDate.format('YYYY-MM-DD'));
This prints the current date and the date 7 days later in a readable format.
Conclusion
These 5 npm packages are super useful when you're starting with Node.js:
✅ nodemon
– Automatically restart the server.
✅ dotenv
– Manage environment variables.
✅ axios
– Make HTTP requests easily.
✅ express
– Build web servers quickly.
✅ moment
– Work with dates without headaches.
If you're new to Node.js, install these packages and try them out. They'll save you time and make development a lot easier!
Have you used any of these? Or do you have a favorite npm package? Let me know in the comments! 😊
#Nodejs #JavaScript #WebDevelopment #npm #CodingForBeginners
Comments
Please login to publish your comment!
By logging in, you agree to our Terms of Service and Privacy Policy.
No comments here!