Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
pnpm 10.x reference skill covering workspaces, catalogs, patches, peer deps, overrides, and CI/CD caching strategies.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/best-practices-migration.md
1---2name: migration-to-pnpm3description: Migrating from npm or Yarn to pnpm with minimal friction4---56# Migration to pnpm78Guide for migrating existing projects from npm or Yarn to pnpm.910## Quick Migration1112### From npm1314```bash15# Remove npm lockfile and node_modules16rm -rf node_modules package-lock.json1718# Install with pnpm19pnpm install20```2122### From Yarn2324```bash25# Remove yarn lockfile and node_modules26rm -rf node_modules yarn.lock2728# Install with pnpm29pnpm install30```3132### Import Existing Lockfile3334pnpm can import existing lockfiles:3536```bash37# Import from npm or yarn lockfile38pnpm import3940# This creates pnpm-lock.yaml from:41# - package-lock.json (npm)42# - yarn.lock (yarn)43# - npm-shrinkwrap.json (npm)44```4546## Handling Common Issues4748### Phantom Dependencies4950pnpm is strict about dependencies. If code imports a package not in `package.json`, it will fail.5152**Problem:**53```js54// Works with npm (hoisted), fails with pnpm55import lodash from 'lodash' // Not in dependencies, installed by another package56```5758**Solution:** Add missing dependencies explicitly:59```bash60pnpm add lodash61```6263### Missing Peer Dependencies6465pnpm reports peer dependency issues by default.6667**Option 1:** Let pnpm auto-install:68```ini69# .npmrc (default in pnpm v8+)70auto-install-peers=true71```7273**Option 2:** Install manually:74```bash75pnpm add react react-dom76```7778**Option 3:** Suppress warnings if acceptable:79```json80{81"pnpm": {82"peerDependencyRules": {83"ignoreMissing": ["react"]84}85}86}87```8889### Symlink Issues9091Some tools don't work with symlinks. Use hoisted mode:9293```ini94# .npmrc95node-linker=hoisted96```9798Or hoist specific packages:99100```ini101public-hoist-pattern[]=*eslint*102public-hoist-pattern[]=*babel*103```104105### Native Module Rebuilds106107If native modules fail, try:108109```bash110# Rebuild all native modules111pnpm rebuild112113# Or reinstall114rm -rf node_modules115pnpm install116```117118## Monorepo Migration119120### From npm Workspaces1211221. Create `pnpm-workspace.yaml`:123```yaml124packages:125- 'packages/*'126```1271282. Update internal dependencies to use workspace protocol:129```json130{131"dependencies": {132"@myorg/utils": "workspace:^"133}134}135```1361373. Install:138```bash139rm -rf node_modules packages/*/node_modules package-lock.json140pnpm install141```142143### From Yarn Workspaces1441451. Remove Yarn-specific files:146```bash147rm yarn.lock .yarnrc.yml148rm -rf .yarn149```1501512. Create `pnpm-workspace.yaml` matching `workspaces` in package.json:152```yaml153packages:154- 'packages/*'155```1561573. Update `package.json` - remove Yarn workspace config if not needed:158```json159{160// Remove "workspaces" field (optional, pnpm uses pnpm-workspace.yaml)161}162```1631644. Convert workspace references:165```json166// From Yarn167"@myorg/utils": "*"168169// To pnpm170"@myorg/utils": "workspace:*"171```172173### From Lerna174175pnpm can replace Lerna for most use cases:176177```bash178# Lerna: run script in all packages179lerna run build180181# pnpm equivalent182pnpm -r run build183184# Lerna: run in specific package185lerna run build --scope=@myorg/app186187# pnpm equivalent188pnpm --filter @myorg/app run build189190# Lerna: publish191lerna publish192193# pnpm: use changesets instead194pnpm add -Dw @changesets/cli195pnpm changeset196pnpm changeset version197pnpm publish -r198```199200## Configuration Migration201202### .npmrc Settings203204Most npm/Yarn settings work in pnpm's `.npmrc`:205206```ini207# Registry settings (same as npm)208registry=https://registry.npmjs.org/209@myorg:registry=https://npm.myorg.com/210211# Auth tokens (same as npm)212//registry.npmjs.org/:_authToken=${NPM_TOKEN}213214# pnpm-specific additions215auto-install-peers=true216strict-peer-dependencies=false217```218219### Scripts Migration220221Most scripts work unchanged. Update pnpm-specific patterns:222223```json224{225"scripts": {226// npm: recursive scripts227"build:all": "npm run build --workspaces",228// pnpm: use -r flag229"build:all": "pnpm -r run build",230231// npm: run in specific workspace232"dev:app": "npm run dev -w packages/app",233// pnpm: use --filter234"dev:app": "pnpm --filter @myorg/app run dev"235}236}237```238239## CI/CD Migration240241Update CI configuration:242243```yaml244# Before (npm)245- run: npm ci246247# After (pnpm)248- uses: pnpm/action-setup@v4249- run: pnpm install --frozen-lockfile250```251252Add to `package.json` for Corepack:253```json254{255"packageManager": "[email protected]"256}257```258259## Gradual Migration260261For large projects, migrate gradually:2622631. **Start with CI**: Use pnpm in CI, keep npm/yarn locally2642. **Add pnpm-lock.yaml**: Run `pnpm import` to create lockfile2653. **Test thoroughly**: Ensure builds work with pnpm2664. **Update documentation**: Update README, CONTRIBUTING2675. **Remove old files**: Delete old lockfiles after team adoption268269## Rollback Plan270271If migration causes issues:272273```bash274# Remove pnpm files275rm -rf node_modules pnpm-lock.yaml pnpm-workspace.yaml276277# Restore npm278npm install279280# Or restore Yarn281yarn install282```283284Keep old lockfile in git history for easy rollback.285286<!--287Source references:288- https://pnpm.io/installation289- https://pnpm.io/cli/import290- https://pnpm.io/limitations291-->292