Learn the concept
Package Managers & Dependency Management
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.
npm Basics:
Key Files:
package.json: Project metadata, dependencies, scriptspackage-lock.json: Exact versions for reproducible installsnode_modules/: Installed packagesCommon Commands:
npm init: Create package.jsonnpm install: Install all dependenciesnpm install <pkg>: Add dependencynpm install -D <pkg>: Add dev dependencynpm update: Update packagesnpm run <script>: Run script# 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 startUsing `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.
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.
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.
Create a basic Node.js Express application. Use `npm init` and `npm install express` to set up the project and manage its sole dependency.
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.
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.
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.