DEV Community

Drishti Saraf
Drishti Saraf

Posted on

📦 How I Published My First npm Package: `how-much-time-ago`

npmI've always wondered how to publish a package on npm.

It seemed like one of those things “real devs” did — and I wasn’t sure where to start. But this week, I finally did it! I created and published my very first npm package: how-much-time-ago — a tiny utility that converts dates or timestamps into human-readable relative time strings like:

  • just now
  • 2 minutes ago
  • 3 days ago
  • 1 year ago

In this post, I’ll walk you through how I built and published it — step by step — so if you’ve ever been curious about sharing your own code on npm, this is for you.


đź§  The Idea

I wanted a simple way to show relative times like “5 minutes ago” or “2 days ago” in my projects. There are libraries out there, but I wanted something lightweight, dependency-free, and easy to use in both Node.js and frontend apps.

So I wrote it myself.


đź”§ Step-by-Step: Publishing an npm Package

Here’s exactly how I did it — and how you can too.

1. Create Your Project Folder

mkdir how-much-time-ago && cd how-much-time-ago
npm init -y
Enter fullscreen mode Exit fullscreen mode

Update your package.json with a good name, description, and author info:

{
  "name": "how-much-time-ago",
  "version": "1.0.0",
  "description": "A lightweight utility to show relative time like '2 hours ago'",
  "main": "index.js",
  ...
}
Enter fullscreen mode Exit fullscreen mode

2. Write Your Utility Code

Here’s a minimal version of what’s inside index.js:

function howMuchTimeAgo(input) {
  const date = new Date(input);
  const seconds = Math.floor((Date.now() - date.getTime()) / 1000);

  const intervals = {
    year: 31536000,
    month: 2592000,
    day: 86400,
    hour: 3600,
    minute: 60,
  };

  for (let [unit, value] of Object.entries(intervals)) {
    const count = Math.floor(seconds / value);
    if (count >= 1) return `${count} ${unit}${count > 1 ? 's' : ''} ago`;
  }

  return seconds < 10 ? 'just now' : `${seconds} seconds ago`;
}

module.exports = howMuchTimeAgo;
Enter fullscreen mode Exit fullscreen mode

3. Add a README.md

Write a clear README so other devs can understand and use your package easily:

how-much-time-ago

đź•’ A tiny utility to convert dates or timestamps into human-readable relative time.

Installation

npm install how-much-time-ago
Enter fullscreen mode Exit fullscreen mode

Usage

const timeAgo = require('how-much-time-ago');

console.log(timeAgo(new Date(Date.now() - 90 * 1000))); // "1 minute ago"
console.log(timeAgo('2023-01-01')); // "1 year ago"
Enter fullscreen mode Exit fullscreen mode

Output Examples

  • just now
  • 30 seconds ago
  • 5 minutes ago
  • 3 hours ago
  • 2 days ago
  • 6 months ago
  • 1 year ago

4. Login and Publish to npm

First, login (create an npm account if you don’t have one):

npm login
Enter fullscreen mode Exit fullscreen mode

Then publish your package:

npm publish
Enter fullscreen mode Exit fullscreen mode

That’s it! Your package is live 🚀

👉 View it on npm


đź§Ş Optional: Test Locally First

Before publishing, you can test your package with:

npm link
# Then in another project:
npm link how-much-time-ago
Enter fullscreen mode Exit fullscreen mode

This simulates installing the package globally without publishing.


đź’ˇ Final Thoughts

  • If you've ever been curious about npm publishing — just try it!
  • A good name, clear README, and working code is all you need to start
  • Keep your first package simple, and iterate from there

Want to Try It?

npm install how-much-time-ago
Enter fullscreen mode Exit fullscreen mode

Then:

const timeAgo = require('how-much-time-ago');
console.log(timeAgo('2024-12-01')); // e.g., "4 months ago"
Enter fullscreen mode Exit fullscreen mode

🔜 Coming Soon

I’m planning to add:

  • Optional future time handling (“in 5 minutes”)

Open to feedback and contributions!

👉 GitHub repo

Top comments (0)

OSZAR »