프로젝트를 한 번이라도 진행해봤다면, package.json이라는 파일을 한 번쯤은 보았을 것이다. package.json는 한마디로 그 프로젝트의 모든 정보를 담은 설정 파일이라고 할 수 있다. 프로젝트에 대한 설명, 종속성 패키지, 실행 스크립트 등의 정보들을 알 수 있는 Manifest 파일이다.
매번 프로젝트를 진행할 때마다 package.json에 대해서는 한번도 꼼꼼하게 뜯어본 적이 없어 오늘은 package.json에 대해 톺아보려고 한다.
우선, 나는 거의 모든 프로젝트에서 yarn을 사용하고 있기에 yarn 기준으로 package.json을 정리해보고자 한다. 아래는 yarn에서 설명하는 package.json 공식 문서이다. 본격적으로 공식 문서를 참고해 정리해보자.
📌 package.json 필드 톺아보기
현재 진행 중인 프로젝트 파일 속 package.json과 함께 예시를 들어가며 정리해보자.
{
"name": "chillin-fe",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"cz": "./node_modules/cz-customizable/standalone.js"
},
"dependencies": {
"@emotion/react": "11.11.3",
"@emotion/styled": "11.11.0",
"@headlessui/react": "1.7.18",
"@material-ui/core": "4.12.4",
"@mui/icons-material": "5.15.7",
"@mui/lab": "5.0.0-alpha.165",
"@mui/material": "5.15.9",
"@tanstack/react-query": "5.18.0",
"axios": "^1.6.7",
"clsx": "2.1.0",
"date-fns": "3.3.1",
"next": "14.1.0",
"react": "^18.2.0",
"react-dom": "^18",
"react-hot-toast": "^2.4.1",
"react-query": "3.39.3",
"react-slick": "^0.30.1",
"react-spinners": "0.13.8",
"recoil": "^0.7.7",
"slick-carousel": "^1.8.1",
"tailwind-merge": "2.2.1"
},
"devDependencies": {
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"@types/react-slick": "^0.23.13",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"autoprefixer": "^10.0.1",
"commitizen": "^4.3.0",
"cz-customizable": "^7.0.0",
"eslint": "^8",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^18.0.0",
"eslint-config-next": "14.1.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"postcss": "^8",
"prettier": "^3.2.5",
"tailwindcss": "^3.3.0",
"typescript": "^5"
},
"config": {
"cz-customizable": {
"config": "cz-config.js"
}
}
}
우선 package.json에 필수적으로 필요한 필드는 name
과 version
이다. 이 두 필드가 하나라도 없으면 패키지가 설치될 수 없고 배포 또한 불가능하다.
[Essentials]
{
"name": "chillin-fe",
"version": "0.1.0",
}
위 코드처럼, 패키지의 이름과 버전을 볼 수 있다.
name
version
관련 아티클을 한번 읽어보자.
Software 버전 관리 규칙, 너만 모르는 Semantic versioning
❓Semantic Versioning이란? (semver rule)
우선 이렇게 생겼다. 아마 다들 한 번쯤은 봤을 것이다. iOS 업데이트 시 안 봤을 리가 없다.
Major.Minor.Patch
→ 오픈소스의 버전 구분
[Info]
description
keywords
license
[Links]
homepage
bugs
repository
[Maintainers]
author
contributors
[Files]
files
main
bin
man
directories
[Tasks]
scripts
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"cz": "./node_modules/cz-customizable/standalone.js"
},
config
스크립트에 사용되는 구성 옵션 혹은 매개변수
...
"config": {
"cz-customizable": {
"config": "cz-config.js"
}
}
...
현재 우리 프로젝트에서는 cs-customizable 옵션을 사용하고 있기 때문에 config에 적혀있는 걸 볼 수 있다.
[Dependencies]
아무래도 package.json에서 가장 익숙한 옵션이 바로 Dependencies가 아닐까 싶다.
dependencies
패키지의 개발 및 프로덕션 모든 곳에 필요한 종속성을 말한다.
"dependencies": {
"@emotion/react": "11.11.3",
"@emotion/styled": "11.11.0",
"@headlessui/react": "1.7.18",
"@material-ui/core": "4.12.4",
"@mui/icons-material": "5.15.7",
"@mui/lab": "5.0.0-alpha.165",
"@mui/material": "5.15.9",
"@tanstack/react-query": "5.18.0",
"axios": "^1.6.7",
"clsx": "2.1.0",
"date-fns": "3.3.1",
"next": "14.1.0",
"react": "^18.2.0",
"react-dom": "^18",
"react-hot-toast": "^2.4.1",
"react-query": "3.39.3",
"react-slick": "^0.30.1",
"react-spinners": "0.13.8",
"recoil": "^0.7.7",
"slick-carousel": "^1.8.1",
"tailwind-merge": "2.2.1"
},
항상 버전 앞에 붙는 ‘^’표시가 궁금했었는데 드디어 알았다!
우선 이 ‘^’표시는 캐럿이라고 하며, 아래 사항과 같이 정리할 수 있다.
^1.0.2 : ≥1.0.2 <2.0
^1.0 : ≥1.0.0 <2.0
^1 : ≥1.0.0 <2.0
devDependencies
이 친구는 패키지를 개발할 때만 필요하고 배포 및 프로덕션에는 설치되지 않는 패키지를 말한다.
"devDependencies": {
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"@types/react-slick": "^0.23.13",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"autoprefixer": "^10.0.1",
"commitizen": "^4.3.0",
"cz-customizable": "^7.0.0",
"eslint": "^8",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^18.0.0",
"eslint-config-next": "14.1.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"postcss": "^8",
"prettier": "^3.2.5",
"tailwindcss": "^3.3.0",
"typescript": "^5"
},
여기서 잠깐!
📌 dependencies vs devDependencies 차이를 알고가자.
dependencies에는 주로 애플리케이션 동작과 직접적으로 연관된 라이브러리를 설치하고, devDependencies에는 개발할 때 필요한 라이브러리를 설치한다고 한다.
우선 설치 커맨드는 다음과 같다.
yarn add something
yarn add something --save-dev
or yarn add -D something
애플리케이션에 필요한 라이브러리가 아닌데도 dependencies에 넣게 되면 빌드 시간이 낭비된다. 따라서 devDependencies에 넣어 실제 배포할 때 포함시키지 않도록 하여 빌드 시간을 줄일 수 있으니 항상 이 부분을 유의하면 좋을 것 같다.
devDependencies에 넣으면 좋을 것들
이런 부분도 신경써야 빌드 시간을 효율적으로 사용할 수 있다! 와~
peerDependencies
peerDependenciesMeta
optionalDependencies
bundledDependencies
flat
resolutions
[System]
engines
os
cpu
[Publishing]
private
"private" : true
publishConfig
이렇게 package.json에 어떤 속성들이 있는지 알아보았다. 공식 문서에 더 자세히 나와있기에 중요한 속성들만 좀 구체적으로 작성해보았는데, 평소에 당연하게 사용했던 것들에 대한 이유를 알 수 있는 기회가 된 것 같다.
요즘 잠이 너무 많다!!!!
너무 졸려. . .