Managing Dependencies: npm install and package.json Explained
Master npm install, learn to manage dependencies vs devDependencies, and understand how the node_modules folder powers your Node.js projects.

Previously in this course, we covered Initializing Projects with NPM: The package.json Manifest, where we generated our project’s manifest file. Now that we have a foundation, this lesson adds the ability to expand our project’s capabilities by pulling in third-party code from the vast npm registry.
Package Management from First Principles
In professional development, you rarely write every piece of logic from scratch. Whether you need a web framework like Express or a utility library, you’ll lean on the Node Package Manager (npm).
When you run an install command, npm performs three critical actions:
- Downloads the code: It fetches the requested package from the registry and places it into a folder named
node_modules. - Updates the manifest: It records the package name and version in your
package.jsonfile. - Locks the state: It updates
package-lock.jsonto ensure every developer on your team installs the exact same version of that code.
The Anatomy of npm install
The command npm install (often abbreviated as npm i) is your primary tool. However, it's vital to categorize the packages you install. We divide them into two buckets:
| Category | Description | Common Use Cases |
|---|---|---|
dependencies | Required for your app to run in production. | Express, Mongoose, dotenv |
devDependencies | Required only during development/testing. | Jest, ESLint, Nodemon |
Installing Production Dependencies
When you build a server, it needs specific libraries to handle HTTP requests. To install a package and save it to your dependencies, use the default flag:
Bashnpm install express
Note: In older versions of npm, you had to use --save. Today, npm install saves to your dependencies by default.
Installing Development Dependencies
Some tools are only for you, the developer. For example, nodemon automatically restarts your server when you save a file. You don’t need this running on your production server. Use the --save-dev or -D flag:
Bashnpm install nodemon --save-dev
Worked Example: Building Our Project
We are building a REST API, so let's set up our workspace with the necessary tools. Open your terminal in your project directory and run:
-
Install Express:
Bashnpm install expressCheck your
package.json. You will see a new entry under"dependencies". -
Install a development tool:
Bashnpm install nodemon -DCheck your
package.jsonagain. You will see"devDependencies"created withnodemoninside. -
Verify the
node_modulesfolder: Runls node_modules(ordiron Windows). You will see a directory full of folders. This is where the physical code for your dependencies lives. Never edit files inside this folder directly.
Hands-on Exercise
- Open your terminal inside your project folder.
- Install the
dotenvpackage (used for managing environment variables) as a regular dependency. - Install
jest(a testing framework) as a development dependency. - Open your
package.jsonand confirm that the versions of these packages appear under their respective keys.
Common Pitfalls
- Committing
node_modules: Never commit yournode_modulesfolder to Git. It can contain thousands of files and is platform-specific. Always includenode_modulesin your.gitignorefile. - Misunderstanding
package-lock.json: Beginners often delete this file when they run into issues. Don't! This file ensures consistency. If you have trouble with resolution, look into how to debug dependency conflicts instead. - Global vs Local: Avoid installing packages globally (using the
-gflag) unless they are CLI tools. Always prefer local installations to keep your project portable.
FAQ
What happens if I delete node_modules?
Don't panic! Since your package.json and package-lock.json are saved, you can simply run npm install in the terminal, and npm will download everything needed to get your project back to its original state.
How do I update my packages?
You can run npm update to upgrade packages within the version ranges defined in your package.json.
Why does my node_modules folder look so big?
Dependencies often have their own dependencies. npm installs the entire tree of these requirements, which is why the folder grows quickly. This is normal.
Recap
We’ve learned that npm install is the standard way to add external logic to our projects. By distinguishing between dependencies and devDependencies, we keep our production builds lean and our development environment feature-rich. We’ve also reinforced the importance of the manifest file and the node_modules directory as the hub of our application's ecosystem.
Up next: We will dive into modules, learning how to structure our own code and use the require function to glue it all together.

