NPM & Package Management
Tap into the world's largest software registry! Learn to use npm to install, manage, and share packages.
Tap into the world's largest software registry! Learn to use npm to install, manage, and share packages. This hands-on tutorial focuses on practical implementation of npm & package management concepts.
NPM & Package Management
NPM stands for Node Package Manager. It is the "App Store" for JavaScript developers.
Instead of writing everything from scratch (like date formatting, math functions, or server logic), you can download code written by others.
1. What is NPM? π¦
It consists of three parts:
- The Website: Browse millions of packages (libraries).
- The Registry: A huge database where packages are stored.
- The CLI (Command Line Interface): A tool to install and manage packages.
2. The package.json File π
Every Node.js project starts with this file. It's the ID card of your project.
How to create it:
Run npm init -y in your terminal.
Key Fields:
name: Your project's name.version: Current version (e.g., 1.0.0).scripts: Shortcuts for terminal commands (e.g.,npm start).dependencies: List of external packages your project uses.
3. Installing Packages β¬οΈ
To use a library (like lodash for utility functions), you install it.
Command: npm install <package_name>
This does two things:
- Downloads the code into a folder called
node_modules. - Adds the package name and version to your
package.json.
4. Dependencies vs. DevDependencies ποΈ
Not all packages are needed for your app to run. Some are just for building or testing.
-
Dependencies (
npm install <pkg>):- Required for the app to run (e.g.,
express,react,mongoose). - Installed on the production server.
- Required for the app to run (e.g.,
-
DevDependencies (
npm install -D <pkg>):- Only needed during development (e.g.,
jestfor testing,nodemonfor auto-restart,eslintfor linting). - NOT installed on production.
- Only needed during development (e.g.,
5. Semantic Versioning (SemVer) π·οΈ
Version numbers look like 1.2.3. They have a specific meaning:
- Major (1.x.x): Breaking changes. (Code might stop working).
- Minor (x.2.x): New features (Backward compatible).
- Patch (x.x.3): Bug fixes (Backward compatible).
The Caret (^) Symbol:
In package.json, you'll see "lodash": "^4.17.21".
The ^ means: "It's safe to update Minor and Patch versions, but DO NOT update the Major version."
AI Mentor
Confused about "NPM, package.json, dependencies vs devDependencies, and semantic versioning"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 4Which command creates a package.json file?
Key Takeaways
β
npm init -y starts a project.
β
npm install adds packages.
β
node_modules is where code lives (don't touch it!).
β
DevDependencies are for tools, not the app itself.
β
SemVer keeps your app from breaking when updating.
What's Next?
Now that we can install packages, how do we actually use them? Let's master Node Modules (CommonJS vs ES Modules)!
Keep coding! π