3

How to migrate from yarn / npm to pnpm

 2 years ago
source link: https://dev.to/andreychernykh/yarn-npm-to-pnpm-migration-guide-2n04
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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 for disk usage.

It is also easy to start using pnpm if you have used npm or yarn before because the CLI is very similar.

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", 
  ...
}

This will prevent other devs from accidentally installing dependencies with anything else than pnpm

Step 4: Create pnpm-workspace.yaml

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

Step 4 (a): remove "workspaces" from "package.json", since it's no longer needed.

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)

Important! You need to keep in mind that pnpm doesn’t 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 doesn’t explicitly specify B in the dependencies or devDependencies, then the execution will fail.

Cheatsheet

Tables Commands Cool
Install dependencies pnpm i https://pnpm.io/cli/install
Add a dependency pnpm add <package> https://pnpm.io/cli/add
Shows all packages that depend on the specified package pnpm 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 package pnpm -w <command> https://pnpm.io/pnpm-cli#-w---workspace-root
Restrict commands to specific subsets of packages pnpm --filter <package_selector> <command> https://pnpm.io/filtering
This runs an arbitrary command from each package's "scripts" object pnpm -r <command> https://pnpm.io/cli/run#--recursive--r

Recommend

  • 59
    • www.tuicool.com 5 years ago
    • Cache

    pnpm vs Yarn: monorepo node_modules

    Both pnpm (as of v2.17) and Yarn (as of v1.12) support fast, concurrent installations in monorepos. However, there is a big difference betwee...

  • 5
    • zsqk.github.io 3 years ago
    • Cache

    Node Npm Cnpm Yarn Pnpm | news view

    news view node依赖管理 Node的依赖管理工具 使用一个名为package.json的文件,用户可以通过npm install –save命令把项目里所有...

  • 5

    Welcome to the release notes for Yarn 3.1! We're quite excited by this release, as it brings various improvements that we've all been looking forward to. Let's dig into that! As always, keep in mind those are only the...

  • 4

    在日常开发中我们时常会遇到 node_modules 中的依赖问题,前一次运行出错删除 package-lock.json 和 node_modules 之后重新安装就好了。 或者是明明自己开发的一个库在 A 项目中运行完好,但是安装到 B 项目之后却提示缺少依赖。就算没有遇到上面的两个问题,肯...

  • 24
    • blog.bitsrc.io 2 years ago
    • Cache

    Stop Using NPM/Yarn. Use PNPM Instead

    Stop Using NPM/Yarn. Use PNPM InsteadWhy I switched from NPM/Yarn to PNPM and why you should too

  • 2

    npm,pnpm,yarn,npx的那些事儿更新日期: 2022-09-18阅读: 9标签: npm分享

  • 5

    由 pnpm 做出來的效能比較:npm、yarn 以及 pnpm 找資料的時候看到 pnpm 有在做幾個常見的 javascript package manag...

  • 6
    • ssshooter.com 1 year ago
    • Cache

    速通 npm、yarn、pnpm

    速通 npm、yarn、pnpm速通 npm、yarn、pnpm2023-03-06codingnpm<v3 node 依赖管理器的起源,在 npm 前两个版本中,依赖无限嵌套...

  • 6
    • dev.to 1 year ago
    • Cache

    npm v/s yarn v/s pnpm

  • 5
    • pawelgrzybek.com 9 months ago
    • Cache

    I replaced npm, yarn and nvm with pnpm

    I replaced npm, yarn and nvm with pnpmPublished: 2023.12.01 · 1 minute readI used to use npm as my package manager because I like d...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK