π· What is NPM?
NPM (Node Package Manager) is the default package manager for Node.js. It allows developers to:
- Install libraries/packages (also called modules).
- Share their own packages with others.
- Manage project dependencies.
- Run project-related scripts (like start, test, build).
π NPM is the worldβs largest software registry β with over 1.5 million packages!
π· Why Use NPM?
- π₯ Install ready-to-use open-source libraries.
- π§ Manage and track dependencies in your project.
- π§ͺ Run custom scripts like testing, deployment, etc.
- β»οΈ Use version control for packages.
- π§βπ» Publish your own packages to the npm registry.
π· Installing and Using NPM
NPM is automatically installed when you install Node.js.
π Check version:
npm -v
π· Working with NPM β Common Commands
Command | Description |
---|---|
npm init | Initializes a Node.js project and creates package.json |
npm install <package-name> | Installs a package locally |
npm install -g <package> | Installs a package globally |
npm uninstall <package> | Uninstalls a package |
npm update | Updates all packages |
npm list | Lists all installed packages |
π· Key Files in NPM Projects
π 1. package.json
The heart of any Node.js project. It stores metadata about your project and its dependencies.
Example:
{
"name": "student-app",
"version": "1.0.0",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "^4.18.2"
}
}
π 2. package-lock.json
- Automatically generated when
npm install
runs. - Locks the version of installed packages to ensure consistent builds.
π 3. node_modules/
- Folder where all installed dependencies are stored.
- Should not be manually edited.
- Usually ignored in
.gitignore
.
π· Installing Packages β Examples
β Local Installation:
npm install express
This creates:
node_modules/express/
- Adds entry in
dependencies
section ofpackage.json
β Global Installation:
npm install -g nodemon
Used for tools you want accessible from the command line across projects.
π· Dev Dependencies vs Dependencies
Type | Description | Installed With |
---|---|---|
dependencies | Packages needed to run your app | npm install |
devDependencies | Packages needed only during development/testing | npm install --save-dev |
Example:
npm install jest --save-dev
π· Using Scripts in NPM
In package.json
, you can define shortcuts for tasks:
"scripts": {
"start": "node app.js",
"test": "echo 'Testing...'"
}
Run:
npm start
npm test
You can also add:
npm run build
npm run dev
π· Creating and Publishing Your Own Package (Intro Level)
- Initialize: bashCopyEdit
npm init
- Write your package logic.
- Login: bashCopyEdit
npm login
- Publish: bashCopyEdit
npm publish
π Your package is now available for anyone to install via
npm install your-package-name
.
π· Real-World Popular NPM Packages
Package Name | Description |
---|---|
express | Web framework for Node.js |
mongoose | MongoDB object modeling |
cors | Enable CORS in APIs |
nodemon | Automatically restarts server on changes |
axios | HTTP requests from Node or browser |
dotenv | Manage environment variables |
jsonwebtoken | Used for JWT-based authentication |
π· Benefits of Using NPM in Development
β
Fast and efficient project setup
β
Access to the largest module library
β
Consistent dependency management
β
Easy collaboration and version control
β
Active community and frequent updates
π Suggested Practical Activities
- Create a new project with NPM
- Use
npm init
, install some dependencies.
- Use
- Build a REST API using Express
- Use
npm install express
- Use
- Use
nodemon
for auto-restart- Install as dev dependency
- Write a script in
package.json
to run your app - Explore and install packages from https://www.npmjs.com
π Conclusion
π NPM is not just a tool β itβs a core part of the Node.js development lifecycle.
Mastering NPM is crucial for every modern backend developer, and especially important for MCA students to build real-world, scalable, and efficient applications.