Node Modules란 https://nodejs.org/api/modules.html#loading-from-node_modules-folders
If the module identifier passed to require() is not a built-in module, and does not begin with '/', '../', or './', then Node.js starts at the directory of the current module, and adds /node_modules, and attempts to load the module from that location. Node.js will not append node_modules to a path already ending in node_modules.
If it is not found there, then it moves to the parent directory, and so on, until the root of the file system is reached.
Node Modules는 node.js가 기본으로 제공하는 의존성 관리 방식입니다. 프로젝트에서 require, import를 호출하면 Node.js는 현재 모듈이 위치한 디렉터리부터 상위로 올라가며 node_modules 폴더를 탐색해 모듈을 찾습니다. 다만 모든 패키지를 압축 해제된 파일 형태로 풀어 두기에 설치 시간이 길고 디스크 사용량도 커서 대규모 프로젝트일수록 설치 속도가 상대적으로 느린 편입니다.
PnP란 Plug'n'Play | Yarn
“Yarn PnP works by generating a single Node.js loader file in place of the typical node_modules folder.
This loader file, named .pnp.cjs, contains all information about your project's dependency tree and lets tools resolve require and import calls.”
PnP 방식에서는 “node_modules” 폴더가 사라지고, 패키지가 압축된 ZIP 형태로 .yarn/cache/ 에 저장됩니다. yarn install 시 프로젝트 루트에 .pnp.cjs 로더 파일을 생성합니다. 이 파일이 패키지 경로, 버전 매핑 정보를 메모리에 로드해 require, import 호출을 해석합니다. 로더의 매핑 정보를 활용해 패키지를 찾는 구조입니다.
이를 도표로 비교하면 아래와 같습니다.
항목 | Node Modules | PnP |
설치 구조 | node_modules/ 디렉토리에 모든 의존성 패키지 설치 | node_modules/ 없음. .pnp.cjs 파일로 경로를 매핑함 |
모듈 탐색 | Node.js 기본 규칙 (require, import 시 디렉토리 탐색) | Yarn의 .pnp.cjs를 통해 직접 경로 매핑 |
속도 | 느릴 수 있음 (디스크 I/O가 많음) | 빠름 (파일 수 적고 I/O 줄어듦) |
호환성 | 대부분의 패키지와 완벽하게 호환 | 일부 패키지/툴은 호환성 이슈 있음 |
Next Standalone이란 https://nextjs.org/docs/pages/api-reference/config/next-config-js/output
Next.js can automatically create a standalone folder that copies only the necessary files for a production deployment including select files in node_modules.
Next.js에서 제공하는 빌드 설정입니다. node_modules를 모두 사용하지 않고 production 환경에 필요한 파일만 사용하도록 추출해주는 모드입니다. 빌드된 결과물이 상대적으로 가벼워져서 용량이 줄어듭니다. 이를 통해 docker image의 용량이 줄어들어 배포시에 좀 더 빠르게 빌드/배포가 가능합니다.
결론적으로 Next.js의 도커 이미지 최적화를 적용하려면 Standalone을 사용해야 하고, Standalone은 vercel/nft의 빌드를 차용하고 있어 node modules 방식으로만 의존성 관리가 가능합니다.
'DevOps' 카테고리의 다른 글
Service Discovery란? (0) | 2025.02.23 |
---|---|
DevOps - USE 메소드와 RED 메소드(Monitoring 방법론) (0) | 2025.02.07 |
DevOps - Incident severity level의 정의 (0) | 2025.01.17 |