Where are my npm Packages?
Node Package Manager (npm) is an essential tool for any JavaScript developer working with Node.js or front-end frameworks like React, Angular, or Vue. It allows you to easily install, manage, and share reusable code packages. A common question for beginners (and sometimes even experienced developers) is: where does npm actually install these packages? This tutorial will break down the different locations where npm can install packages, and how to find them on your system.
Global vs. Local Installations
npm packages can be installed in two primary ways: globally and locally. Understanding the difference is key to locating your installed packages.
-
Local Installations: These packages are installed within the
node_modules
directory of your project. This means each project can have its own specific versions of dependencies, isolated from other projects. This is the most common and recommended way to manage dependencies for a project. -
Global Installations: These packages are installed in a central location on your system, accessible from any project. Global installations are typically reserved for command-line tools (like
pm2
,nodemon
, or testing frameworks) that you want to use across multiple projects.
Locating Locally Installed Packages
Finding locally installed packages is straightforward.
-
The
node_modules
Directory: Navigate to your project’s root directory in the terminal. npm automatically creates anode_modules
folder within that directory to store all the local dependencies. -
Listing Local Packages: From within your project directory, run the following command in your terminal:
npm list
This will display a tree-like structure showing all the installed packages and their dependencies.
Locating Globally Installed Packages
Finding globally installed packages requires a few more steps. The location depends on your operating system, but npm provides tools to help you find it.
-
Using
npm root -g
: This command reveals the root directory where npm installs global packages. Open your terminal and run:npm root -g
The output will be a path, typically something like
/usr/local/lib/node_modules
on macOS/Linux orC:\Users\[YourUsername]\AppData\Roaming\npm
on Windows. -
Listing Global Packages: After finding the global installation directory, you can list the installed global packages using:
npm list -g
This will show a tree of globally installed packages. To see only the top-level packages (without their dependencies), use:
npm list -g --depth=0
-
Finding Executables in your PATH: Global packages often include executable commands.
npm bin
displays the directory where these executables are located. To see the global executable directory, run:npm bin -g
It’s important to ensure this directory is added to your system’s
PATH
environment variable so you can run these commands from anywhere in your terminal.
Customizing the Global Installation Path
The default global installation path can be customized using npm’s configuration.
-
Viewing the Current Prefix: To see the currently configured prefix (the root directory for global installations), run:
npm config get prefix
-
Changing the Prefix: You can change the prefix using the following command:
npm config set prefix /path/to/your/new/global/modules
Replace
/path/to/your/new/global/modules
with the desired directory. After changing the prefix, you may need to update yourPATH
environment variable to include the new directory.
Operating System Specifics
-
macOS/Linux: Global packages are typically installed in
/usr/local/lib/node_modules
. -
Windows: Global packages are usually installed in
C:\Users\[YourUsername]\AppData\Roaming\npm
. Note thatAppData
is a hidden folder.
By understanding these concepts and using the provided commands, you can easily locate and manage your npm packages, ensuring a smooth and efficient development workflow.