How to Publish Your First NPM Package

How to Publish Your First NPM Package

What is NPM?

NPM stands for Node Package Manager. It’s a library and registry for JS packages. NPM also has cli tools to help you install the different packages and manage their dependencies.

NPM is free and open-source and have become the center of JavaScript code sharing. There are more than a million packages available on npm.

Why use NPM?

It’s certainly possible to manage your project’s dependencies yourself. As your project grows, though, this can become a herculean task.

This is where a package manager like NPM comes in. NPM solves this problem by handling dependency and package management for your project.

You define all your project’s dependencies inside your package.json file. In the package.json file, you can also specify which versions your project depends upon. This is useful to prevent updates from these packages from breaking your project.

Publishing your package to npm

First, you need to create a new account by visiting the npmjs.com website. The important information is the username, password, and public email. You’ll need this information when you publish your package.

Make the Directory

Let’s create a folder. In your terminal:

mkdir sum-of-array-js
cd sum-of-array-js

Initializing a npm Package

Now that you’re in the folder, run the bellow command

npm init
Blog Image

Running npm init will ask you a few setup questions (for example, the name of your package, your package’s description, etc).

You can simply hit “Enter” for each question and this default boilerplate for package.json file will be created in your directory:

{
  "name": "sum-of-array-js",
  "version": "1.0.0",
  "description": "this package will give the sum of array",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

The only fields that are required in package.json are “name”, “version”, and “main”.

The “main” field is the file path to the entry point JavaScript file. Let’s create the index.js file in our terminal:

touch index.js

now open this folder in favorite code editor and write your function and export it.

const SumofArray = (Array)=> {
    const sum = Array.reduce((partialSum, a) => partialSum + a, 0);
    return sum;
};

export default SumofArray

Creating a README

Generally it’s a good idea to include documentation for your package so others know how to use it. The README file is typically used for this purpose.

Let’s create the README file in the root of your package’s directory:

# create the README file
$ touch README

# put some text into README
$ echo "## sum-of-array-js" > README

Currently, this is what the file directory for sum-of-array-js looks like:

sum-of-array-js
 |_ index.js
 |_ README
 |_ package.json

Essentially, this is the basic structure of a npm package.

Now that we feel pretty good about our package, let’s publish it! to publish the package to npm, from the terminal, you use the npm login command:

npm login

It will prompt you to enter the following information: username, password, and email.

Finally, you run the npm publish command to publish the sum-of-array-js package to the npm registry.

npm publish

If you received an error after running the npm publish command, it’s likely that the package that you were publishing has a name that already exists. In this case, you need to change the package name in the package.json file to something unique.

Using the published package

To use the sum-of-array-js package, you create a new Node project called demo and run the npm init command:

npm init

To install the sum-of-array-js package that you have published, you run the npm install command:

npm install sum-of-array-js

The following creates the app.js and use the sum-of-array-js package:

import SumofArray from "sum-of-array-js"

console.log(SumofArray([1,4,5,7,4,3,6]))

Finally, run the app.js program using the following command:

node app.js

Output:

30

Hopefully this guide has shown you how easy it is to contribute your software to the Open Source community, regardless of how significant or small it might be.