Skip to main content

Command Palette

Search for a command to run...

Create Your First NPM Package!

Updated
4 min read
Create Your First NPM Package!
S
Relentlessly curious and fiercely high‑agency, I'm a senior software engineer who thrives on taking products from zero to production‑ready in record time. Having worked across startups and fast-paced organizations, I've built a reputation as a cracked engineer who solves hard problems fast and owns the entire product stack end-to-end. My knack for rapid problem‑solving and end‑to‑end ownership turns complex ideas into polished, scalable products that ship fast and scale even faster. phewww... I love a cup of coffee ☕ while writing code. So let's connect and have a meet over something you wanna discuss. I'll be more than happy to have it. Book a call with me: https://cal.com/xshivank

Introduction

The Node.js runtime environment's default package manager for JavaScript is called Node Package Manager (NPM). It offers a way to organize, manage, and share packages and code collections for use in Node.js projects.

NPM makes it simple to manage and install packages, freeing developers from worrying about dependencies and allowing them to concentrate on writing code. Developers can reuse and share code with others by using NPM's package sharing and distribution features.

Importance of NPM

NPM has a great deal of importance in the Node.js ecosystem including:

a) Speed & scalability: NPM is really fast and easy to use. A user can perform various functions using NPM.

b) Version Management: NPM provides version management for the packages thereby making it easy to manage different versions of a package and ensuring its compatibility.

c) Package Management: NPM provides a common repository for the packages which can be easily managed and installed. Thus, making the job of developer quite easy.

Various Types Of NPM Install

In the NPM package, you get various commands for installing, updating, and managing the packages. Given below are the various types of npm install commands:

npm install: This is the most common command. It installs the packages specified in the dependencies section of the project's package.json file.

npm install --save-dev: This command installs the specified packages and adds them to the devDependencies section of the package.json file. These are only used for development purposes.

npm install --save: This command installs the specified packages and adds them to the dependencies section of the package.json file.

npm install <package-name> -g: This command installs a package globally so that it can be used for all projects on the same machine.

npm install <package-name>@<version>: This command installs a specific version of a package. The @ symbol is used to specify the version number.

npm install <package-name>@<latest>: This command installs the latest version of a package.

Flags Which Can Be Used With The npm install Flag

Some flags which can be used are given below:

g or global: It installs the package globally so that it can be used for all projects on the same machine.

-save: It adds the package to the dependencies section of the package.json file.

-save-dev: It adds the specified package to the devDependencies section of the package.json file.

f or force: It forces the installation of a package, even if it would have skipped due to compatibility issues.

-no-optional: It installs only the required dependencies and not the optional ones.

-save-optional or S: It installs the optional dependencies and adds them to the optionalDependencies section of the package.json file.

-no-save: It prevents the package from being added to the dependencies or devDependencies section of the package.json file.

Let's Try With an Example

Let us consider a project wherein you need to install the following package:

a) Express: It is a Node.js framework.

b) MongoDB: It is a document database.

To install these packages, follow the given steps:

  1. Initialize npm: By initializing npm, it will create a package.json file. Now, you can initialize npm by running the given command:
  $  npm init -y

The output will be:

  {
    "name": "<name-of-project>",
    "version": "<version-of-project>",
    "description": "",
    "main": "<default-run-file>", 
    "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC"
  }
  1. Installation: Now you'll need to install express and mongoDB globally and to do that run the given below commands:
$  npm install express

This will create a dependency object in the package.json file.

{
    ......
    ......,
    "dependencies": {
    "express": "^4.18.2"
  }
}

Similarly, try to run the same commands for the installation of MongoDB.

To sum up, I'd say that learning the creation of NPM packages is really important in order to become a Node.js developer. Therefore, try executing the given commands and practice as much as you can!

I hope that you must have found this article quite helpful. If yes, then do give a read to some of my other articles!

Who knows you might become a great programmer 🤔!

More from this blog

Shivank Kapur

57 posts

learning distributed systems 🦀 / dev-rel eng @routerprotocol / member @superteam / previously @thedogecapital @pushprotocol