Node.js Managing Dependencies
🌟 Node.js – Managing Dependencies
In Node.js, dependencies are external packages/modules that your project needs to run.
NPM (Node Package Manager) is used to install, update, remove, and manage these dependencies.
Dependency info is stored in:
package.json → main dependency list
package-lock.json → locks exact versions
node_modules/ → installed package files
✅ 1. Install a Dependency
A. Install a package (for production)
This adds it to:
B. Install a package for development
Adds to:
✅ 2. Install a Specific Version
✅ 3. Update Dependencies
Update a single package
Update all packages
Check which packages are outdated
It shows:
| Package | Current | Wanted | Latest | Location |
✅ 4. Remove a Dependency
This removes it from node_modules and package.json.
✅ 5. Install All Dependencies from package.json
When cloning a project from GitHub:
This installs everything listed in:
dependencies
devDependencies
✅ 6. Global vs Local Dependencies
Local Modules (default)
Installed inside your project folder.
Global Modules
Available system-wide.
Check global modules:
✅ 7. Versioning Rules in Dependencies
In package.json:
Example:
^ caret → allow minor & patch updates
~ tilde → allow only patch updates
none → exact version
* → any version
✅ 8. Peer Dependencies
Used when a package requires another package to be installed by the user (framework plugins, libraries, etc.).
Example:
✅ 9. Optional Dependencies
Not required; installation continues even if they fail.
Example:
✅ 10. Checking Dependency Tree
Or global:
🔥 Complete Example of Managing Dependencies
Install packages:
Install dev tools:
Update:
Remove:
Install dependencies from project:
🎯 Summary
| Task | Command |
|---|---|
| Install package | npm install package |
| Install dev package | npm install package --save-dev |
| Remove | npm uninstall package |
| Update | npm update |
| Check outdated | npm outdated |
| Install all | npm install |
| Install exact version | npm install package@1.0.0 |
