StoryMarch 4, 20242 min read

How I Created A Simple NPM Package

Have you ever wanted to create your own NPM package but felt intimidated by the process?


How I Created A Simple NPM Package

Photo by Leone Venter on Unsplash

Hello Everyone!

Have you ever wanted to create your own NPM package but felt intimidated by the process?

Well, I was in the same boat not too long ago.

However, after diving in and experimenting, I realized that creating your own package can be a fun and rewarding experience, even for beginners like me.

In this story, I’ll walk you through my journey of creating a simple NPM package.

Getting Started

First things first,

I had to come up with an idea for my package.

I wanted to start with something small and manageable, so I decided to create a package that generates random quotes.

I figured this would be a fun project to work on and would also be useful for others who might want to add random quotes to their websites or applications.

Setting Up the Project

Next,

I set up a new directory for my project and initialized it as an NPM package using `npm init`.

This command prompted me to provide some information about my package, such as its name, version, and description.

Once I had filled in all the necessary details, NPM generated a `package.json` file for me, which serves as the configuration file for my package.

Writing the Code

With the project set up, it was time to start writing some code.

I created a new JavaScript file called `index.js` and started by defining a function that would return a random quote from an array of quotes.

Here’s what my code looked like:

const quotes = [
  "The only way to do great work is to love what you do. - Steve Jobs",
  "Stay hungry, stay foolish. - Steve Jobs",
  "Innovation distinguishes between a leader and a follower. - Steve Jobs",
  // Add more quotes here
];

function getRandomQuote() {
  const randomIndex = Math.floor(Math.random() * quotes.length);
  return quotes[randomIndex];
}

module.exports = getRandomQuote;

Testing the Package Locally

Before publishing my package to the NPM registry, I wanted to make sure it was working as expected.

I created a simple test file called `test.js` where I imported my `getRandomQuote` function and called it to display a random quote.

After running the test script using `node test.js`, I was happy to see that it was indeed returning random quotes as expected.

Publishing to NPM

Finally,

it was time to share my package with the world! I logged in to my NPM account using the `npm login` command and then used the `npm publish` command to publish my package to the NPM registry.

Within a few seconds, my package was live and available for anyone to install using `npm install`.

Creating my own NPM package turned out to be a lot easier than I had initially thought.

By breaking down the process into smaller steps and taking it one step at a time,

I was able to create a simple yet functional package that I could share with others.

Whether you’re a seasoned developer or just starting out, I encourage you to give it a try – you might be surprised at what you can accomplish!

Thank you for taking the time to explore this topic with me😃

If you have any questions please don’t hesitate to reach out