Table of contents
1.
Introduction
2.
Installing all Dependencies
3.
Installing a Single Package
4.
Updating Packages
5.
Versioning
6.
Running Tasks
7.
Frequently Asked Questions
8.
Key Takeaways
Last Updated: Mar 27, 2024

Introduction to npm

Author kabir singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Node.js has a standard package manager, and this manager is generally termed npm.

Npm is the most significant single language code on this planet and has 350000 packages in its registry.

It had started as a quick way to download and manage dependencies of Node.js packages, but now it has since become a tool that is readily used also in frontend JavaScript.

So this article will take you through the entire concept and knowledge of npm. Let's see what npm does and what it consists of.

Installing all Dependencies

Npm manages all the downloading and installing of dependencies to our project.

If our project has a json file, i.e., something like package.json, we can easily install everything that our project needs by running just one line of code. And all this happens in the nodes_modules folder, which is created automatically if it is not there.

And the code is as follows:

npm install

 Let’s now have a look at how we can install specific packages. 

Installing a Single Package

So using the below code, we can install specific packages.

npm install <package-name>

 

 

Before version 5 of npm, we had to add an extra flag --save to install a specific package successfully.

Pretty often, we see more flags attached to this command, and these flags are as follows:

  • --save-dev is used to install and add the entry to the package.json file devDependencies.
  • --no--save is used to install but doesn’t add the entry to the package.json file Dependencies.
  • --save--optional is used to install and add the entry to the package.json file optionalDependencies.
  • --no-optional prevents optional dependencies from being installed.


There are shorthands of the flags as well which can be used:

-S: --save

-D: --save-dev

-O: --save-optional

 

 

 

 

Now let’s have a quick look at how we can update packages.

Updating Packages

Updating is also pretty much easy, and it can be done using the code:

npm update

 

 

Here npm checks all the packages for a newer version that satisfy our version constraints.

We can also update a specific package, and that can be done using the below-mentioned code:

npm update <package-name>

 

Versioning

Npm does not only stick with plain downloads. It manages versioning too. This helps us to specify a specific package or a version higher or lower than the current version.

So we know about installing, updating, and versioning in npm, but how do we run stuff? Let's see how we can run tasks.

Running Tasks

Any specific task can be run by using the following command :

npm run <task-name>

 

 

Let’s see this with an example:

{

  "scripts": {

    "start-dev""node lib/server-development",

    "start""node lib/server-production"

  }

}

 

It’s a feature that is pretty much commonly used to run webpack:

{

  "scripts": {

    "watch""webpack --watch --progress --colors --config webpack.conf.js",

    "dev""webpack --progress --colors --config webpack.conf.js",

    "prod""NODE_ENV=production webpack -p --config webpack.conf.js",

  }

}

 

But these commands are pretty long, and we tend to forget them, so instead, we can use the below mentioned commands and run shorthands:

$ npm run watch

$ npm run dev

$ npm run prod

 

Since we have learned about npm properly, let's look at the frequently asked questions and try answering them.

Frequently Asked Questions

Question 1: What is npm?

Answer: Npm is the biggest single language code on this planet and has a total of 350000 packages in its registry.

 

Question 2: How can we install all required packages at once?

Answer: we can install all packages using the given statement 

npm install

 

 

 

Question 3: What is a package-lock?

Answer: Package-lock can be defined as the large list of each dependency listed in our package.
 

Question 4: What does npm stand for?

Answer: npm stands for Node Package Manager.

Key Takeaways

In this article, we get an excellent understanding of npm and how we can install different packages using its code. It also tells us how we can update stuff and also run different commands.

Liked the blog? And want to learn more about Node JS? Read it here.

Recommended Readings:

NPX vs NPM

Please have a look and also visit our platform Coding Ninjas Studio for more interview problems!

Happy learning, Ninjas!

 

Live masterclass