
Npm in NodeJs
Great question! Letβs break down NPM (Node Package Manager) β a core part of working with Node.js π
π¦ What is NPM?
NPM is the default package manager for Node.js.
It helps you:
π₯ Install libraries (aka packages/modules)
βοΈ Manage project dependencies
π Publish your own packages (if you want!)
β Check NPM Version
npm -v
π Common NPM Commands
Command | What It Does |
---|---|
npm init | Create a new package.json (manual setup) |
npm init -y | Auto-generate default package.json |
npm install <package> | Install a package locally |
npm install -g <package> | Install a package globally |
npm uninstall <package> | Remove a package |
npm update | Update all dependencies |
npm list | List installed packages |
π package.json
β Whatβs Inside?
This file tracks your project's dependencies, version, scripts, and more.
{ "name": "my-app", "version": "1.0.0", "dependencies": { "express": "^4.18.2" }}
π₯ Installing a Package Example
npm install express
β
Adds express
to:
node_modules/
package.json
β under"dependencies"
π§Ό Install Dev-Only Packages
Use --save-dev
for things like testing or building tools:
npm install nodemon --save-dev
β‘οΈ Goes into "devDependencies"
π‘ Run Scripts with npm
Inside package.json
:
"scripts": { "start": "node app.js", "dev": "nodemon app.js"}
Then run:
npm run startnpm run dev
π Using Global Packages
Install tools like nodemon
or http-server
globally:
npm install -g nodemon
Then you can run nodemon app.js
from anywhere.
π NPM Registry
You can search or explore packages on:
π https://www.npmjs.com