Understanding npm install: How It Works and Where Packages Are Installed
npm install
is a command used in the terminal to add libraries or packages to a Node.js project. Here's what happens step by step:
-
Find the Package: When you run
npm install <package-name>
, npm looks for the package in the npm registry, which is a large database of public and private packages. -
Download the Package: If the package is found, npm downloads it to your local machine.
-
Install the Package: npm then installs the package into your project. Specifically, it places the package inside the
node_modules
directory in your project folder. This directory is where all your project's dependencies (libraries and packages your project needs) are stored. This directory is automatically created by npm if it doesn't exist when you first install a package. -
Update
package.json
andpackage-lock.json
: If you usenpm install <package-name> --save
(for npm versions < 5, since npm 5 and later do this automatically), npm adds the package to your project'spackage.json
file underdependencies
ordevDependencies
(if you use-save-dev
). This file keeps track of which packages and versions your project depends on. Thepackage-lock.json
file is also updated to ensure a consistent installation of package versions across environments.
Can You Remove a Library Completely from a Project?
Yes, you can remove a library completely from your project.
Run npm uninstall <package-name>
in your terminal. This command removes the package from the node_modules
directory and also updates your package.json
and package-lock.json
files to reflect the removal.
Get my free, weekly JavaScript tutorials
Want to improve your JavaScript fluency?
Every week, I send a new full-length JavaScript article to thousands of developers. Learn about asynchronous programming, closures, and best practices — as well as general tips for software engineers.
Join today, and level up your JavaScript every Sunday!
Thank you, Taha, for your amazing newsletter. I’m really benefiting from the valuable insights and tips you share.