Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 29, 2022 08:17 pm GMT

How to migrate from yarn / npm to pnpm

Motivation

pnpm is more performant at fetching, resolving, and storing dependencies. My personal experience shows that in some projects pnpm can be approx. 10x time faster at resolving dependencies and up to 3x more efficient at disk usage.
It is also easy to migrate to from npm or yarn. And the CLI is very similar to npm and yarn, too
https://pnpm.io/motivation

Migration guide

Step 1: Install pnpm Installation
Step 2: Delete node_modules

npx npkill

Step 3: Add to package.json

"scripts": {  "preinstall": "npx only-allow pnpm",   ...},"packageManager": "pnpm@<whatever pnpm version you're going to use>"

Step 4: Create pnpm-workspace.yaml

packages:  # include packages in subdirs (e.g. apps/ and components/)  - "apps/**"  - 'packages/**'  # if required, exclude some directories  - '!**/test/**'

Step 5: Run

pnpm import

This command will create a pnpm-lock.yaml file based on yarn.lock (or packages-lock.json)
Step 6: Remove yarn.lock (or packages-lock.json)
Step 7: Install dependencies

pnpm i

Step 8: Replace npm run (or yarn) to pnpm in all package.json and other files (E.g. pnpm test instead of npm run test)

To run all commands across all workspaces in a monorepo use pnpm run | pnpm (e.g. pnpm -r test)

Important! You need to keep in mind that pnpm doesnt use dependency hoisting:

When installing dependencies with npm or Yarn Classic, all packages are hoisted to the root of the modules directory. As a result, source code has access to dependencies that are not added as dependencies to the project.
By default, pnpm uses symlinks to add only the direct dependencies of the project into the root of the modules directory.
pnpm

In practice it means that if you have a package A that imports a package B (import something from 'B') but doesnt explicitly specify B in the dependencies or devDependencies, then the execution will fail.

Cheatsheet

TablesCommandsCool
Install dependenciespnpm ihttps://pnpm.io/cli/install
Add a dependencypnpm add <package>https://pnpm.io/cli/add
Shows all packages that depend on the specified packagepnpm why <package>https://pnpm.io/cli/why
Run a command as if it was executed form the root of the project rather than a workspace packagepnpm -w <command>https://pnpm.io/pnpm-cli#-w---workspace-root
Restrict commands to specific subsets of packagespnpm --filter <package_selector> <command>https://pnpm.io/filtering
This runs an arbitrary command from each package's "scripts" objectpnpm -r <command>https://pnpm.io/cli/run#--recursive--r

Original Link: https://dev.to/andreychernykh/yarn-npm-to-pnpm-migration-guide-2n04

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To