Have you heard about Envirotechnical? Let's fight climate change one byte at a time :) Learn More →

logo
Published on

How I built a NPM Package (and why you should too!)

Authors
Table of Contents

What is NPM

NPM, which name cleverly changes every time you open the webpage to search for packages, is, as the object of your search implies, a Package Manager for Node, or, better still - Node Package Manager.

Whenever you do a command like the following

npm i package

You are, in fact, using this package manager to download and install the dependency you wanted to install in the first place.

That's nice and all, but you come to a point where just using the platform is not enough and you want to know: how the heck do those people put a library on npm? Is it hard?

It's not hard, it's actually quite simple and built-in for NodeJs.

My first package deployed on NPM was npackr, a utility CLI that can be used to simplify readability of package.json files.

# The --read flag just prints the script commands on your stdout
# while the --explain command shows you what those npm scripts would run.
# Running just the CLI command allows you to choose directly what command
# you would like to run
$ npackr [--read [--explain]]

This is a screenshot for the console output once you run the command without flags:

npackr npm command cli for package.json

So why did I build this CLI (command line interface) tool? Because I found myself dealing with a lot of different projects and everytime I couldn't remember exactly what commands those projects implemented.

I found myself repeating this commands and it felt like a waste of time and effort for such a simple task.

$ cat package.json
$ less package.json
$ cat package.json | grep scripts

They did work, but you had to read, you had to find the command and repeat the process by writing the command on the console.

I wanted to waste less time doing that and that's how npackr came to be.

Now I can read the scripts directly without creating weird aliases or chaining unix commands and most importantly, I can run the scripts directly from the npackr instance that gets created.

$ npackr
? Which command would you like to run? (--read for only reading the commands) (U
se arrow keys)
  ──────────────
❯ start
  dev
  build
  serve
  analyze
  lint
(Move up and down to reveal more choices)

This was a small and yet so welcomed feature!

Building NPM Package from scratch

To create a NPM package you need to follow some simple steps:

  1. create a NPM account
  2. Sign up to NPM through your console with npm login

Now, you probably built your project locally so you should check that your package.json file has the following metadata, necessary for our package to go live on NPM.

{
    "name": "your_package",
    "version": "1.0.0",
    "description": "A good description for your package",
    "repository": {
        "type": "git",
        "url": "https://github.com/your-username/repository.git"
    },
    "keywords": [
        "keywords",
        "other keywords"
    ],
    "author": "Your name",
    "license": "MIT"
}

You don't actually need all of them, the name and version would be enough, but what's the point in getting your package out there if you don't sponsor yourself with some good optimization a little!

Is SEO for npm even a thing? If you know, send me a message!

$ npm publish

That's all it takes!

You might think that this can't be true, I mean, that's three commands, and yet it is just like that!

What takes time is the building of the actual library or utility, but once you have it and you make sure your package.json is written as it is meant to, you can just publish your package on npm without any worries!

Need to udpate your package? Just republish it with a new version!

The goodbye

I hope you found this article useful and to your liking and if you have any requests, drop a message on one of my social media accounts or open an issue/start a discussion on github, on this repository!

As always you can find me on Twitter, listen to my Podcast on Spotify and add me on LinkedIn to talk professionally (yeah, right)