동기

디스크 공간 절약#

pnpm 콘텐츠 주소 지정 스토어의 그림. 그림에는 node_modules가 있는 두 개의 프로젝트가 있습니다. The files in the node_modules directories are hard links to the same files in the content-addressable store.

npm을 사용할 때 종속성을 사용하는 프로젝트가 100개 있는 경우 해당 종속성의 사본 100개가 디스크에 저장됩니다. pnpm을 사용하면 의존성이 content-addressable 저장소에 저장되므로, 다음을 따릅니다.

  1. 다른 버전의 의존성에 여러분이 의존하는 경우, 다른 파일만이 저장소에 추가됩니다. 예를 들어, 파일이 100개 있고 새 버전에서 그중 한 파일만 변경되었다면, pnpm update는 하나의 변경 때문에 종속성 전체를 복제하는 대신 새 파일 1개만 스토어에 추가합니다.
  2. 모든 파일은 디스크 상에서 단일 위치에 저장됩니다. When packages are installed, their files are hard-linked or cloned (copy-on-write) from that single place, consuming no additional disk space. By default, pnpm copies files only when the filesystem supports neither. See packageImportMethod. 이를 통해 프로젝트 간에 동일한 버전의 의존성을 공유할 수 있습니다.

As a result, you save a lot of space on your disk proportional to the number of projects and dependencies, and you have a lot faster installations.

설치 속도 향상#

pnpm은 세 단계로 설치를 수행합니다:

  1. 의존성 해결: 필요한 모든 의존성을 식별해 저장소로 가져옵니다.
  2. 디렉터리 구조 계산: 종속성을 기반으로 node_modules 디렉터리 구조가 계산됩니다.
  3. 의존성 연결: 나머지 모든 종속성을 가져와 스토어에서 node_modules로 하드 링크합니다.

pnpm 설치 과정의 그림. Packages are resolved, fetched, and hard linked as soon as possible.

Each package moves to the next stage as soon as it is ready, without waiting for the other packages. This is faster than the traditional three-stage installation process, which resolves all dependencies, then fetches all of them, then writes all of them to node_modules.

An illustration of how package managers like Yarn Classic or npm install dependencies.

평탄하지 않은 node_modules 디렉토리 생성#

npm 또는 Yarn 클래식을 통해 의존성을 설치할 때, 모든 패키지는 모듈 디렉토리의 루트로 호이스트됩니다. 결과적으로, 소스 코드는 프로젝트에 의존성으로 추가되지 않은 의존성에 접근할 수 있습니다.

기본적으로, pnpm은 symlink를 사용하여 프로젝트의 직접적인 의존성만을 모듈 디렉토리의 루트로 추가합니다.

pnpm이 생성한 node_modules 디렉터리의 그림. Packages in the root node_modules are symlinks to directories inside the node_modules/.pnpm directory

독특한 pnpm의 node_modules 구조와 이 구조가 Node.js 생태계에서 잘 작동하는 이유에 대해 자세히 알고 싶다면 다음을 읽어보세요:

팁

사용 중인 도구가 심볼릭 링크와 잘 작동하지 않는 경우에도 pnpm을 사용할 수 있으며, nodeLinker 설정을 hoisted로 지정하면 됩니다. 이것은 pnpm이 npm 및 Yarn 클래식에서 생성한 것과 유사하게 node_modules 디렉토리를 생성하도록 지시합니다.