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

npm -v

Check Node.js version:

node -v

2️⃣ Initialize a New Node.js Project

Creates package.json:

npm init

For quick setup (no questions):

npm init -y

3️⃣ Installing Packages

There are three types of installs:


📌 A. Local Installation (most common)

npm install express

or shorthand:

npm i express

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)

npm install express --save

OR in modern npm:

npm install express

Automatically goes to:



 


📌 C. Save to devDependencies (development only)

Example: nodemon, typescript

npm install nodemon --save-dev

Gets added to:



 


4️⃣ Install Packages Globally

Global packages are installed system-wide.

Example: nodemon CLI tool

npm install -g nodemon

Check global packages:

npm list -g

5️⃣ Uninstall Packages

Local uninstall:

npm uninstall express

Global uninstall:

npm uninstall -g nodemon

6️⃣ Updating Packages

Update a package:

npm update express

Update all:

npm update

7️⃣ Installing Specific Versions

npm install express@4.18.2

8️⃣ NPM Scripts

You can run custom scripts defined in package.json:

package.json



 

Run:

npm run start

OR

npm run dev

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:

npm install

⭐ BONUS: Useful NPM Commands

CommandPurpose
npm initCreate project
npm installInstall dependencies
npm install packageInstall package
npm uninstall packageRemove package
npm updateUpdate dependencies
npm listList installed packages
npm outdatedCheck outdated packages
npm cache clean --forceClear npm cache

NPM vs NPX

ToolPurpose
npmInstalls packages
npxRuns packages without installing

Example:

npx create-react-app myapp

You may also like...