Let's build our first JavaScript npm package!

iamvarandas

André Varandas

Posted on August 18, 2020

Let's build our first JavaScript npm package!

About JavaScript packages

You've probably already used some JavaScript libraries, scripts, blocks of code you found online or you might even already built your own library. The need for a package comes when we want to reuse and share code and functionalities with others.

A package, usually, is just a set of functions and/or variables or full-fledged apps and cli's that are made available for us to build our own.

To make it easier for us, fortunately, nodeJS has multiple systems that allow us to maintain, manage, and share these scripts - so they can be enclosed in a "package". The most common or famous one is NPM (Node Package Manager).

Let's say that you want to share some functionality between multiple projects. For this, we can write the code we need, "package" and push it to npm to distribute it.

The project that we are going to work on might not be of any use right now, but it's a good example of a simple JavaScript package - we want to share some functionality between projects and let NPM manage that for us - so we don't have to copy-paste code everywhere.

About our package - mood-sentences

In this series, we will build a simple package and push it to npm registry. We will cover everything from the very beginning of creating a new git repository to publishing our code to the package manager.

Project idea 💡

Note: To be honest with you, I haven't searched for this package. But it probably already exists. 😏

Imagine that we have a Discord chat bot and we need to send some messages when someone joins the server. We could write an array of sentences like "Welcome to the server John 🎉" and then randomly pick one, or, we could write a JavaScript package that exports multiple sentences for different moods as we need them.

This would enable us to focus on the mood sentences, share them with others, reuse in other projects, and hopefully receive some nice Pull Requests with more sentences and moods. 🤓

We will write some sentences into a JSON file, that reflects multiple moods, starting by adding some sentences that can be used to express:

Anger, excitement, boredom and happiness

Then we will add a few methods to return sentences.

The full project is available on Github, and has multiple branches, each one, corresponds to a chapter of this series.

Thanks for reading, in the next part we will start adding our own dependencies and preparing everything to work on our package.

💖 💪 🙅 🚩
iamvarandas
André Varandas

Posted on August 18, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Setting up continuous Integration
javascript Setting up continuous Integration

August 18, 2020

Writing tests
javascript Writing tests

August 18, 2020

Writing code
javascript Writing code

August 18, 2020

Initial Setup
javascript Initial Setup

August 18, 2020