pnpm vs npm

npm의 flat tree#

npm maintains a flattened dependency tree. 이로 인해 디스크 공간 낭비는 줄어들지만, 부작용으로 node_modules 디렉터리가 지저분해집니다.

반면 pnpm은 디스크상의 전역 콘텐츠 주소 지정 스토어에 대한 하드 링크와 심볼릭 링크를 사용하여 node_modules를 관리합니다. 덕분에 디스크 공간을 훨씬 적게 사용하면서도 node_modules를 깔끔하게 유지할 수 있습니다. 더 알고 싶다면 스토어 구조에 대한 문서를 참조하세요.

올바른 pnpm node_modules 구조의 장점은 프로젝트의 package.json에 명시되지 않은 모듈을 사용할 수 없게 하여 "어리석은 버그를 방지하는 데 도움이 된다"는 점입니다.

설치하기#

pnpm은 package.json에 저장하지 않고 패키지를 설치하는 것을 허용하지 않습니다. pnpm add에 매개변수를 전달하지 않으면 패키지는 일반 종속성으로 저장됩니다. 기존의 npm과 마찬가지로, --save-dev 및 --save-optional을 사용하여 패키지를 개발 종속성 또는 선택적 종속성으로 설치할 수 있습니다.

이러한 제약의 결과로 dependency를 삭제하여 분리된 상태로 남겨두지 않는 한, 프로젝트에는 관련 없는 패키지는 존재하지 않을 것 입니다. 그래서 pnpm의 prune 명령 구현에서는 정리할 패키지를 지정할 수 없으며, 불필요한 패키지와 고아 패키지를 항상 모두 제거합니다.

디렉토리 dependencies#

A directory dependency points to a directory in the filesystem. pnpm handles it according to its protocol:

  • link: (also recorded by pnpm add ./dir) symlinks the directory into node_modules. pnpm does not install the directory's own dependencies.
  • file: copies the directory into the virtual store as a package and installs its dependencies like any other package.

For example, if a package called foo (<root>/foo) has bar@link:../bar as a dependency, pnpm does not install the dependencies of <root>/bar when you run pnpm install on foo.

모노레포처럼 여러 패키지에서 동시에 설치를 실행해야 하는 경우, pnpm -r에 대한 문서를 참조하세요.