본문 바로가기
Backend

Node.js 프로젝트 시작하기 | package.json

by 신림쥐 2024. 5. 3.
728x90
반응형

 

 

     


     

    개요

    Node.js 프로젝트를 시작할 때 가장 먼저 생성하는 파일이 package.json 입니다. 이 파일은 프로젝트 정보를 저장하고, 의존성 관리 및 개발 환경 설정을 담당합니다.

    이번 포스팅에서는 package.json의 주요 설정 항목을 정리해보겠습니다.

     

    package.json

    package.json은 Node.js 프로젝트의 의존성, 스크립트 및 메타데이터를 저장하는 파일입니다.

     

    package.json 역할

    • 프로젝트 정보 (이름, 버전, 설명 등)
    • 패키지(라이브러리) 및 의존성 관리
    • 실행할 명령어(스크립트) 설정
    • Node.js 버전 요구 사항 지정

     

    package.json 생성하기

    Node.js 프로젝트에서 다음 명령어를 실행하여 pckage.json 파일을 생성한다.

    $ npm init
    
    This utility will walk you through creating a package.json file.
    It only covers the most common items, and tries to guess sensible defaults.
    
    See `npm help init` for definitive documentation on these fields
    and exactly what they do.
    
    Use `npm install <pkg>` afterwards to install a package and
    save it as a dependency in the package.json file.
    
    Press ^C at any time to quit.
    package name: (react-app)
    version: (1.0.0)
    description:
    entry point: (index.js)
    test command:
    git repository:
    keywords:
    author:
    license: (ISC)
    About to write to C:\workspace\react\ReactFrontendStudy\base-app\cc\package.json:
    
    {
      "name": "react-app",
      "version": "1.0.0",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC",
      "description": ""
    }
    
    
    Is this OK? (yes)
    
    npm notice
    npm notice New major version of npm available! 10.9.0 -> 11.2.0
    npm notice Changelog: https://github.com/npm/cli/releases/tag/v11.2.0
    npm notice To update run: npm install -g npm@11.2.0
    npm notice

     

    npm init 명령어를 입력하면 초기 설정되는 환경은 다음과 같습니다.

     

    • "name" → 프로젝트 이름
    • "version" → 프로젝트 버전
    • "main" → 진입점 파일
    • "script" →  자주 사용하는 명령어를 정의
    • "author" → 개발자 정보
    • "license" → 라이선스 유형
    • "description" → 프로젝트 설명

     

    {
      "name": "react-app",
      "version": "1.0.0",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC",
      "description": ""
    }

     

     

    package.json 구성 요소

    scripts

    package.json의 scripts 섹션은 자주 사용하는 명령어를 정의할 수 있는 곳입니다. 예를 들어, start와 dev 스크립트를 설정한다.

    {
      "scripts": {
        "start": "node server.js",
        "dev": "nodemon server.js"
      }
    }

     

    dependencies, devDependencies

    Node.js 프로젝트에 외부 라이브러리(패키지)를 의존성을 관리하는 곳입니다. 패키지 유형별로 dependencies와 devDependencies에 구분하여 관리할 수 있습니다.

     

    dependencies

    • 앱에서 가장 일반적으로 사용하는 종속성 패키지
    • 실제 런타임에서 사용되는 패키지
    • 패키지가 실행되는 데 필요한 라이브러리들이 자동으로 설치됨

    devDependencies

    • 런타임에서는 필요하지 않은 패키지
    • 빌드 타임이나 개발 모드에서만 필요한 패키지

    peerDependencies

    • 특정 버전의 다른 라이브러리와 호환되는 패키지를 정의
    • 플러그인 형태의 라이브러리에서 주로 사용
    • 패키지에서 직접 require(import) 하지는 않지만, 호환성이 필요한 경우 사용
    • 자동으로 설치되지 않으며, 사용자가 직접 설치해야 함

    optionalDependencies

    • 선택적으로 설치되는 패키지
    • 설치가 실패해도 애플리케이션 실행에는 문제가 없음
    {
      "dependencies": {
        "builtin-modules": "^1.1.1",
        "lodash.cond": "^4.2.0",
        "lodash.find": "^4.2.0",
        "pkg-up": "^1.0.0"
      },
      "devDependencies": {
        "ava": "^0.13.0",
        "eslint": "^2.4.0",
        "eslint-plugin-ava": "^1.3.0",
        "xo": "^0.13.0"
      },
      "optionalDependencies": {
        "lodash.isarray": "^4.0.0"
      },
      "peerDependencies": {
        "react": ">=15.0.0 <16.0.0"
      },
      "bundledDependencies": [
        "@generated/foo",
      ]
    }

     

    repository

    프로젝트의 Git 저장소 정보 추가

    "repository": {
      "type": "git",
      "url": "git+https://github.com/username/my-node-app.git"
    }

     

    engines

    Node.js 버전 요구 사항을 설정한다.

     

    • "node": ">=16.0.0" → 최소 Node.js 16 이상이 필요함을 지정합니다.
    • "npm": ">=8.0.0" → 최소 npm 8 이상이 필요함을 지정합니다.
    • "engineStrict": true → 지정된 버전이 아니면 실행 불가

     

    {
      "engines": {
        "node": ">=16.0.0",
        "npm": ">=8.0.0"
      },
      "engineStrict": true
    }

     


    기타 설정

    • "private": true → 프로젝트를 npm에 공개하지 않도록 설정
    • "keywords" → 프로젝트와 관련된 키워드
    {
      "private": true,
      "keywords": ["node.js", "express", "api"]
    }

     

    728x90
    반응형