Introduction to npm

npm(node package manager) is regarded as the standard package manager used in javascript. The npm registry crossed a million packages last year(Jun ’19). It is the largest single-language code repository. That being said, it is a little obvious now that npm is a big deal .

npm is automatically installed when you install Node.js

Since its a package manager, it is used to manage downloads and handle dependencies of your project.

It is a common saying that

There is a package for everything in npm

Well, almost everything. There’s always more.

Let’s have a walkthrough of npm from introduction to how to publish your own package .

Introduction

npm is written entirely in JavaScript(JS) and was developed by Isaac Schlueter . It was initially used to download and manage dependencies, but it has since also used frequently in frontend JavaScript.

npm can manage packages that are local dependencies of a particular project, as well as globally-installed JavaScript tools. In addition to plain downloads, npm also manages versioning, so you can install any version, higher or lower according to the needs of your project. If no version is mentioned, the latest version of the package is installed.

How to use

→ If package.json file exists in your project directory, all you need to do is use this command

npm install

This command will initialize the node_modules folder and install all packages that the project needs.

And if you need to update the installed packages, hit

npm update

All packages will be updated to their latest versions.

→ If you just need to install a single package, you can use this command

npm install <package_name>

Similarly, if you just need to update a single package, all you need to do is

npm update <package_name>

Note: By default, packages are installed in the local scope. If you need to install the package at global scope, you need to mention the flag -g

npm install <package_name> -g

This will install the package at the global scope in the system directory.

How to publish your own package

Several developers feel the need to use some functionality of one project in another. Normally, the developer copies code from one project and pastes in another but if it is common functionality that may be used in several projects its a better and good practice to publish your reusable codes as npm packages.

Also, why miss a good opportunity to become an author, right?

Okay, so let’s start!

1. npm init

When you are in your desired directory, open the CLI, and use this command:

npm init

A text utility appears and you are asked to enter a few details to create a basic package.json file such as package name, version, description, author, etc.

Enter the details of the package as you please. The defaults will be mentioned in parentheses like this →

package name: (test-pkg)
version: (1.0.0)
description: This is a test package
.
.
. 

Just hit Enter if you want the default options.

A basic package.json will be created looking something like this →

{
  "name": "test-pkg",
  "version": "1.0.0",
  "description": "This is a test package",
  "main": "index.js",
  "author": "after-academy",
  "license": "ISC"
}

Obviously, you can change the package.json file later if you want.

2. Source

Now, you need to prepare the source code. If the code is not too big, it is typically just write in the main file( index.js here). Else, conventionally a src directory is used where your abstract code is stored in several files.

★ Remember to export the code using module.exports

3. Test

You now need to thoroughly test your code before publishing. This is how you, as a developer, confirm that your package can actually be used.

4. Publish

If this is your first time, you probably won’t be logged in with your npm account. If you don’t have an account, go to npmjs.com and create your account.

Now, in your terminal, log in with your npm account.

npm login

And now to publish your package,

npm publish

And you’re done!

You can now check your published project at www.npmjs.com/<username>/<pkg-name>.

The details regarding the package such as version, author name, description, etc. are extracted from the package.json file you created earlier.


Now you’ve got a basic idea about what npm is and how you can publish your own package to the npm registry.

Please give your feedback in the comments below.

Happy Learning!