곰돌푸우❤️

목차

    package-lock.json 파일이 무엇인가?

     

    node.js 프로젝트를 진행하면서 package-lock.json 파일이 자꾸 생겨 귀찮게 느껴왔었습니다. 제거해도 다시 생성되고 굳이 commit에 포함시키지 않았었습니다. 계속 무시만 할 수 없어서 package-lock.json 파일에 대해서 알아보았습니다.

     

    프로젝트에 설치된 노드 모듈들의 의존성 트리를 기록하고 있습니다.
    package-lock.json

    {
      "name": "my-nodejs",
      "version": "0.0.0",
      "lockfileVersion": 1,
      "requires": true,
      "dependencies": {
        "express": {
          "version": "4.16.4",
          "resolved": "https://registry.npmjs.org/express/-/express-4.16.4.tgz",
          "integrity": "sha512-j12Uuyb4FMrd/qQAm6uCHAkPtO8FDTRJZBDd5D2KOL2eLaz1yUNdUB/NOIyq0iU4q4cFarsUCrnFDPBcnksuOg==",
          "requires": {
            "accepts": "~1.3.5",
            "array-flatten": "1.1.1",
            "body-parser": "1.18.3",
            "content-disposition": "0.5.2",
            "content-type": "~1.0.4",
            "cookie": "0.3.1",
            "cookie-signature": "1.0.6",
            "debug": "2.6.9",
            "depd": "~1.1.2",
            "encodeurl": "~1.0.2",
            "escape-html": "~1.0.3",
            "etag": "~1.8.1",
            "finalhandler": "1.1.1",
            "fresh": "0.5.2",
            "merge-descriptors": "1.0.1",
            "methods": "~1.1.2",
            "on-finished": "~2.3.0",
            "parseurl": "~1.3.2",
            "path-to-regexp": "0.1.7",
            "proxy-addr": "~2.0.4",
            "qs": "6.5.2",
            "range-parser": "~1.2.0",
            "safe-buffer": "5.1.2",
            "send": "0.16.2",
            "serve-static": "1.13.2",
            "setprototypeof": "1.1.0",
            "statuses": "~1.4.0",
            "type-is": "~1.6.16",
            "utils-merge": "1.0.1",
            "vary": "~1.1.2"
          }
        },
        "body-parser": {
               ...
        }, 
        ...
      }
    }

    위 내용을보면 express 모듈이 4.16.4 버전으로 설치되어 있는 것을 확인할 수 있습니다. 이렇게 프로젝트에 설치된 모듈들의 의존성 트리를 잠그고(lock) 있다가 다음에 npm install 로 모듈들을 재설치할 때 모두 동일한 버전으로 설치됨을 보장합니다. 즉 새로운 버전이 업데이트 되어도 최종적으로 설치되어 개발됐던 위의 버전들로 재설치가 됩니다.

    package-lock.json 이 유용한 예

    git 저장소에 node.js 프로젝트를 커밋할 때 보통 node_modules 폴더는 부피가 커서 commit에 포함시키지 않습니다.이 때 package-lock.json 파일은 꼭 함께 커밋을 해주어야 합니다. 다른 팀원이 이 프로젝트를 처음 pull 받아서 npm install 을 했을 때 개발 당시의 동일한 버전의 모듈들이 설치됨을 보장하기 위함입니다.

    프로젝트에 처음 express 모듈을 설치(npm install --save express)하면 package.json 파일에 "express": "~4.16.1" 처럼 Tilde Ranges 표기법으로 버전이 명시됩니다. 이후에 package-lock.json 파일이 없는 상황에서 node install 명령어로 로컬에 모듈을 설치할 때 4.16.1 버전보다 높은 버전이 npm에 publish 되어 있다면 높은 버전의 express 모듈이 로컬에 설치됩니다. 이는 개발당시의 버전과 달라져 예기치 않던 버그가 발생할 수 있습니다. 따라서 package-lock.json은 의존성 관리를 위해 필수이며 꼭 git 저장소에 commit 해야하는 대상입니다.

    facebook twitter googleplus kakaostory naver