Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Installations
2.1.
Local Installation
2.2.
Global Installation 
3.
Frequently Asked Question
4.
Key Takeaways
Last Updated: Mar 27, 2024

Where does npm install the packages?

Author Kabir Singh
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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.

And here, we will be looking at the actual places where these packages have also been installed and the types of installations that take place.

Let’s now have a look at the types of installations.

Installations

Whenever we wish to install packages with the help of npm, we can use two ways to perform the installation. The two types are as follows:

  • Local Installation
  • Global Installation

 

While using the default installation technique, the code written below comes in handy:

npm install lodash

 

This lets our packages install in the current file tree, and it comes under the node_modules folder.

As we go by this process, there is one more addition that happens in the backend: the lodash entry in our dependencies property in the package.json file that is present in the current folder.

Since we have seen the default installation now, let's look at the global installation process as well.

Local Installation

By default, when you type an npm install command, like:

npm install lodash

 

It is referred to as local installation and is installed in the current file tree, under the node_modules subfolder.

As this happens, npm also adds the lodash entry in the dependencies property of the package.json file present in the current folder.

Global Installation 

Now, this global installation takes place with the help of a specific flag that is the -g flag:

Let’s have a better understanding by looking at the code given below

npm install -g lodash

When this process takes place, rather than installing the package in a local folder, npm chooses a global location.

Where exactly does that location exist? Let’s see!

Using a specific command, we can see where the location exists on our particular machine. And the command is :

npm root -g

 

Now on different operating systems, the location differs in the following ways:

  • On Linux or macOS the location could look like this : /usr/local/lib/node_modules
  • On Windows, it might look like this: C:\Users\YOU\AppData\Roaming\npm\node_modules

 

If you end up using nvm for managing your Node.js versions, then the location differs in that case.

For example using nvm the packages might look something like : 

/Users/kabir/.nvm/versions/node/v8.9.0/lib/node_modules

 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Question

  1. How many packages are there on npm?
    npm registry has over 1.3 million packages available.
     
  2. How can we install all required packages at once?
    we can install all packages using the given statement 
    npm install
     
  3. What is a package-lock?
    Package-lock can be defined as the extensive list of each dependency listed in our package.
     
  4. What does npm stand for?
    npm stands for Node Package Manager.

Key Takeaways

This article went through the types of installations and spotted the actual locations where the packages are installed. 

Liked the blog? And want to learn more about Node JS? Please 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!

Previous article
Introduction to Servers and Clients
Next article
Setting up Node Server on Linux
Live masterclass