Node.js NPM
Node.js NPM (Node Package Manager)
NPM stands for Node Package Manager.
It is the world’s largest software registry, used to install, manage, and share JavaScript packages.
Whenever you install Node.js → NPM is automatically installed.
⭐ What Is NPM Used For?
✔ Installing packages (libraries, frameworks)
✔ Managing project dependencies
✔ Running scripts
✔ Sharing your own packages
✔ Creating and managing Node.js projects
1️⃣ Check NPM Version
Check Node.js version:
2️⃣ Initialize a New Node.js Project
Creates package.json:
For quick setup (no questions):
3️⃣ Installing Packages
There are three types of installs:
📌 A. Local Installation (most common)
or shorthand:
Creates:
node_modules/Adds entry in
package-lock.json
But NOT added to package.json unless you add --save (older versions) or use the next method.
📌 B. Save to dependencies (for production use)
OR in modern npm:
Automatically goes to:
📌 C. Save to devDependencies (development only)
Example: nodemon, typescript
Gets added to:
4️⃣ Install Packages Globally
Global packages are installed system-wide.
Example: nodemon CLI tool
Check global packages:
5️⃣ Uninstall Packages
Local uninstall:
Global uninstall:
6️⃣ Updating Packages
Update a package:
Update all:
7️⃣ Installing Specific Versions
8️⃣ NPM Scripts
You can run custom scripts defined in package.json:
package.json
Run:
OR
9️⃣ Understanding package.json
package.json stores project configuration:
🔟 package-lock.json
Auto-generated file.
It stores the exact versions installed.
✔ Ensures consistent installation
✔ Used during deployments
1️⃣1️⃣ node_modules Folder
Stores all installed dependencies.
⚠ Never upload this folder to GitHub
Use .gitignore
To rebuild dependencies:
⭐ BONUS: Useful NPM Commands
| Command | Purpose |
|---|---|
npm init | Create project |
npm install | Install dependencies |
npm install package | Install package |
npm uninstall package | Remove package |
npm update | Update dependencies |
npm list | List installed packages |
npm outdated | Check outdated packages |
npm cache clean --force | Clear npm cache |
NPM vs NPX
| Tool | Purpose |
|---|---|
| npm | Installs packages |
| npx | Runs packages without installing |
Example:
