JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionstooling
Next

Learn the concept

Package Managers & Dependency Management

tooling
junior
package-managers

What is npm and how do you manage packages with it?

npm
package-manager
dependencies
package.json
Quick Answer

npm (Node Package Manager) is JavaScript's default package manager for installing, updating, and managing dependencies. Key files are package.json (project metadata) and package-lock.json (exact versions). Use npm install, npm update, and npm uninstall for management.

Detailed Explanation

npm Basics:

  • Default Node.js package manager
  • Manages project dependencies
  • Runs scripts defined in package.json
  • Accesses npm registry (largest package registry)

Key Files:

  • package.json: Project metadata, dependencies, scripts
  • package-lock.json: Exact versions for reproducible installs
  • node_modules/: Installed packages

Common Commands:

  • npm init: Create package.json
  • npm install: Install all dependencies
  • npm install <pkg>: Add dependency
  • npm install -D <pkg>: Add dev dependency
  • npm update: Update packages
  • npm run <script>: Run script

Code Examples

Common npm commandsBash
# Initialize new project
npm init -y

# Install all dependencies from package.json
npm install
# or shorter
npm i

# Install specific package
npm install lodash
npm i lodash

# Install as dev dependency
npm install --save-dev jest
npm i -D jest

# Install specific version
npm install react@18.2.0

# Install globally
npm install -g typescript

# Update packages
npm update           # Update all
npm update lodash    # Update specific

# Remove package
npm uninstall lodash

# List installed packages
npm list             # All
npm list --depth=0   # Only direct deps

# Check for outdated
npm outdated

# Run scripts
npm run build
npm run test
npm start           # Shorthand for npm run start

Real-World Applications

Use Cases

Starting a new web development project and setting up its initial dependencies

Using `npm init` to create a `package.json` file and then `npm install <package-name>` to add libraries like React, Express, or Lodash, establishing the project's foundation.

Collaborating on a project where multiple developers need to ensure consistent package versions

Relying on `package-lock.json` to guarantee that `npm install` produces an identical `node_modules` structure across all development environments, preventing 'works on my machine' issues.

Automating build, test, and deployment tasks in a project's CI/CD pipeline

Defining custom scripts in `package.json` (e.g., `"build": "webpack"`, `"test": "jest"`) which can then be executed with `npm run build` or `npm test`, streamlining the development and deployment process.

Mini Projects

Simple Node.js API with Express

beginner

Create a basic Node.js Express application. Use `npm init` and `npm install express` to set up the project and manage its sole dependency.

Frontend Build Workflow with npm Scripts

intermediate

Set up a simple frontend project. Define npm scripts in `package.json` for tasks like `start`, `build` (using a tool like Parcel), `lint`, and `test` to automate the development workflow.

Industry Examples

Any JavaScript/Node.js project

npm is the de facto standard for package management in the JavaScript ecosystem. Virtually all companies building web applications, Node.js backends, or any JavaScript-based software use npm (or a compatible alternative like Yarn/pnpm) to manage their project dependencies.

Meta (Facebook)

While Meta developed Yarn as an alternative, npm remains a fundamental tool within their JavaScript infrastructure, especially for managing a vast array of open-source projects and internal libraries. They often integrate npm scripts into complex monorepo setups.

Resources

npm Documentation

docs
Next
What is ESLint and why should you use it?
Next