npm, Yarn and pnpm Tutorial
Learn npm, Yarn and pnpm step by step: 36 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is a Package Manager? Learn what a package manager does, why JavaScript projects depend on hundreds of packages, and how npm, Yarn and pnpm fit together with the registry and node_modules.
- Node.js, npm and Version Managers Install Node.js and npm, check versions, understand LTS releases, switch Node versions with nvm or fnm, and enable Yarn and pnpm through Corepack.
- npm, Yarn and pnpm Compared Compare npm, Yarn Classic, Yarn Berry and pnpm on disk use, strictness, lockfiles and workspaces, with a command translation table and advice on choosing one.
- Your First package.json Create your first package.json with npm init, yarn init or pnpm init, learn what each generated field means, and read and validate the file with plain Node.js code.
Packages and package.json
- package.json Fields Reference A practical tour of package.json fields beyond the basics, including engines, files, bin, repository, bugs, homepage, funding, sideEffects and packageManager, with runnable checks.
- Dependencies, devDependencies and peerDependencies Understand the five dependency fields in package.json, when each applies, how production installs skip devDependencies, and how peer and optional dependencies behave.
- How node_modules and Resolution Work See how Node.js finds a package by walking up node_modules folders, why npm hoists packages, how pnpm uses symlinks, and how to inspect resolution with require.resolve.
- Entry Points, exports and type Learn how main, module, type and the exports map decide what import and require load from a package, including conditional exports, subpath exports and dual ESM/CommonJS packages.
- The npm Registry and Package Names Learn how the npm registry stores packages, how scoped names like @scope/name work, what a package document and tarball contain, and how to use other or private registries with .npmrc.
Installing and Versions
- Installing and Removing Packages Learn npm install, npm uninstall and npm ci with their Yarn and pnpm equivalents, the flags that matter, what each command changes on disk, and how to read install output.
- Semantic Versioning (semver) Understand semantic versioning, what MAJOR, MINOR and PATCH promise, how pre-release tags and build metadata work, and how to compare and sort versions correctly with code.
- Version Ranges: Caret, Tilde and More Learn how caret (^), tilde (~), comparison, x-range, hyphen and OR ranges select versions in package.json, with a runnable range matcher and examples for 0.x versions and pre-releases.
- Updating and Outdated Packages Learn how to find outdated dependencies with npm outdated, update within ranges, upgrade across majors, use npm-check-updates, yarn upgrade-interactive and pnpm update --latest, and avoid breakage.
- Global Installs and npx Learn when to install packages globally, where global packages live, how to fix EACCES permission errors, and how npx, yarn dlx and pnpm dlx run command line tools without installing them.
Scripts and Workspaces
- npm Scripts Learn how the scripts field works, how npm run, yarn and pnpm run scripts, the special start and test commands, passing arguments, chaining commands and how node_modules/.bin is added to PATH.
- Lifecycle Scripts and Hooks Understand pre and post script hooks, the install-time lifecycle (preinstall, install, postinstall, prepare), when each runs for you and your users, and why install scripts are a security concern.
- Environment Variables and Cross-Platform Scripts Set and read environment variables in npm scripts, use cross-env, dotenv and node --env-file, avoid shell-specific commands, and write scripts that work on Windows, macOS and Linux.
- Workspaces Basics Learn what workspaces are, how to declare them in npm, Yarn and pnpm, how local packages link to each other, the workspace protocol, and how to run scripts across all packages.
Publishing
- Preparing a Package for Publishing Get a package ready for the registry. Set name, version, exports, files, types, engines and license, and check the manifest with a small readiness linter.
- Versioning and the Release Workflow Learn semantic versioning for package authors, the npm version command, pre-release tags, lifecycle scripts like prepublishOnly, and a safe release checklist.
- npm pack and publish See what npm pack puts in the tarball, how files and .npmignore work, how integrity hashes are made, and how to publish with npm, Yarn and pnpm safely.
- Scopes, Registries and .npmrc Understand scoped packages, private and mirror registries, dist-tags, access control and the .npmrc, .yarnrc.yml and pnpm settings that point tools at them.
Lockfiles and Security
- Lockfiles Explained Learn why lockfiles exist, what package-lock.json, yarn.lock and pnpm-lock.yaml record, how npm ci and frozen installs work, and how to fix merge conflicts.
- npm audit and Vulnerabilities Learn how npm audit, yarn npm audit and pnpm audit find vulnerable dependencies, how to read severity and paths, when audit fix is safe, and how to triage noise.
- Supply-Chain Security Learn how attackers abuse dependencies and how to defend: install scripts, typosquatting, lockfile review, provenance, two-factor authentication and safer CI settings.
- Overrides and Resolutions Force a specific version of a transitive dependency with npm overrides, Yarn resolutions and pnpm overrides. Learn the syntax, scoping, risks and when to remove them.
Monorepos
- Monorepo Basics and Workspace Layout Learn what a monorepo is, when to use one, how workspaces are declared in npm, Yarn and pnpm, and how a root install links local packages together.
- Workspace Protocol and Local Linking Reference sibling packages with the workspace protocol in Yarn and pnpm, use * ranges in npm, see how versions are rewritten on publish, and link packages outside a monorepo.
- Filtering and Task Orchestration Run scripts in the right workspaces and the right order. Learn npm -w, pnpm --filter, Yarn foreach, affected builds, task pipelines and caching with Turborepo and Nx.
Performance and Alternatives
- node_modules Layouts, Hoisting and the pnpm Store Understand how npm hoists dependencies into a flat node_modules, why phantom dependencies happen, and how pnpm uses a content-addressable store with hard links and symlinks.
- Speeding Up Installs and Caching Make installs fast: understand the package cache, offline and prefer-offline modes, CI cache keys, registry mirrors, and settings that cut install time and noise.
- Corepack, Bun and Other Alternatives Pin the package manager with the packageManager field and Corepack, translate commands between npm, Yarn, pnpm and Bun, and compare the alternatives honestly.
Projects and Practice
- Project: Publish a Small CLI Build a small command-line tool, wire it up with the bin field and a shebang, test it, check the tarball contents and walk through a safe first publish.
- Project: A Mini Dependency Resolver Build a tiny package manager core in Node.js. Parse semver ranges, resolve a dependency tree against a fake registry, write a lockfile and lay out a hoisted node_modules.
- Project: A Monorepo Release Pipeline Model a monorepo release flow in Node.js. Collect change files, compute version bumps with dependents, write changelogs, rewrite workspace ranges and publish in dependency order.
- Practice: Exercises and Cheat Sheet Practice publishing, lockfiles, security and monorepos with runnable exercises, then keep a cheat sheet of commands and a table of common install errors and their fixes.