Installing and managing npm packages is super easy, we're all familiar with the npm install command, but that changes once you get to the creating part, because creating your packages is super-duper hard and complicated! Oh noooo 😱 ...just kidding, it's a breeze — let's dive in.

Here's what you're going to need:

  • An npmjs.com account
  • A machine that has nodejs installed

Initiate a new project with npm

First make a directory and cd into it from your terminal, it doesn't matter what you name it.

We need that sweet sweet package.json file for our plan to work, so let's create it by typing this short command

npm init

Alright, a screen like this should appear:

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (npmpost)

You can specify a unique name and press ENTER for the rest of these questions and it will fill them out automatically for you, by the end of the questionnaire you will end up with a package.json file that looks something like this:

{
  "name": "SUPER_UNIQUE_NAME_HERE",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "author": "",
  "license": "ISC"
}

NOTE: I removed the scripts section, because we don't need it. Also notice the main is set to index.js by default, let's leave it that way.

Alright, let's create an index.js file and add a line of code to it. This is going to be a a very useful package, it will return the square of the number provided. Groundbreaking stuff.

module.exports.square = number => number * number

Now that we have our package.json and our index.js file, we're ready to deploy our package.

Authenticate with npm

Let's authenticate with npm from the command line. Type

npm adduser

and specify your username, password, and email address. (make sure to verify your email address)

PRO TIP: You can type npm whoami to check the account that is currently authenticated with npm

Publish your package to the npm registry

You can easily publish your package by executing the publish command

npm publish

After a bit of processing your package should be live on the npm registry.

Check it out by going to npmjs.com, it should be listed on your profile.

Congratulations on your first npm package 🎉

You can use it in your projects installing it with the good old npm install

npm install <YOUR_PACKAGE_NAME> --save

And you can make use of it like this

const { square } = require( "YOUR_PACKAGE" );

console.log( square( 42 ) );

BONUS TIPS 🎁

Increase the version in your package.json file before publishing your changes, or do it from the terminal.

npm version 1.0.1

You can add description to your package page with a README.md file.

You can link your GitHub repository to your package, by specifying it in your package.json file.