Skip to content

Node Package Manager (NPM)

πŸ”· 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

CommandDescription
npm initInitializes 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 updateUpdates all packages
npm listLists 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 of package.json

βœ… Global Installation:

npm install -g nodemon

Used for tools you want accessible from the command line across projects.


πŸ”· Dev Dependencies vs Dependencies

TypeDescriptionInstalled With
dependenciesPackages needed to run your appnpm install
devDependenciesPackages needed only during development/testingnpm 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)

  1. Initialize: bashCopyEditnpm init
  2. Write your package logic.
  3. Login: bashCopyEditnpm login
  4. Publish: bashCopyEditnpm publish

πŸ“ Your package is now available for anyone to install via npm install your-package-name.


πŸ”· Real-World Popular NPM Packages

Package NameDescription
expressWeb framework for Node.js
mongooseMongoDB object modeling
corsEnable CORS in APIs
nodemonAutomatically restarts server on changes
axiosHTTP requests from Node or browser
dotenvManage environment variables
jsonwebtokenUsed 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

  1. Create a new project with NPM
    • Use npm init, install some dependencies.
  2. Build a REST API using Express
    • Use npm install express
  3. Use nodemon for auto-restart
    • Install as dev dependency
  4. Write a script in package.json to run your app
  5. 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.