곰돌푸우❤️

목차

    Node.js 프로젝트를 할 때 가장 많이 사용하는 것은 npmpackage.json파일입니다.

    npm(node pacakage manager)
    node 모듈 다운로드 및 관리를 하기 위한 프로그램이며 package.json파일을 읽어 node.js로 작성 된 프로그램을 실행시킵니다.

    package.json에는
    프로젝트에 사용되는 모든 모듈명과 버전을 기록하고, 라이센스, 작성자 정보, 스크립트 등을 기록합니다.
    node.js 프로젝트 설정파일인 셈입니다.

    ※ node.js프로젝트의 루트에보면 package.json이라는 파일을 하나씩 가지고 있을겁니다.

     

     

     

    프로젝트 초기화
    node.js 프로젝트 폴더로 진입해서 npm init 을 입력한다.

    seojaeryong:getting-started seo$ 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 json` for definitive documentation on these fields
    and exactly what they do.
    Use `npm install <pkg> --save` afterwards to install a package and
    save it as a dependency in the package.json file.
    Press ^C at any time to quit.
    name: (getting-started) 
    version: (1.0.0) 
    description: study project
    entry point: (index.js) 
    test command: 
    git repository: 
    keywords: 
    author: simsi6
    license: (ISC) 
    About to write to /Users/seo/Workspaces/Nodejs/getting-started/package.json:
    {
      "name": "getting-started",
      "version": "1.0.0",
      "description": "study project",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "simsi6",
      "license": "ISC"
    }
    Is this ok? (yes) yes
    seojaeryong:getting-started seo$ 


    각 설정 항목들이 물음 형식으로 한 단계씩 진행되는데 아무것도 입력없이 엔터만 치면 괄호() 안의 기본값들이 셋팅된다. 최종적으로 모든 설정이 끝나면 생성 될 package.json파일의 경로와 내용이 출력되고 yes를 입력하면 저장과 함께 종료된다.


    필요한 Module 설치
    개발에 필요한 모듈이 있으면 npm install [모듈명] --save 명령어 입력으로 모듈 다운로드 및 설치를 합니다. --save 옵션을 붙이면 package.json파일 안에 모듈 정보를 자동으로 업데이트 해줍니다.

     

     

    seojaeryong:getting-started seo$ npm install express --save
    getting-started@1.0.0 /Users/seo/Workspaces/Nodejs/getting-started
    └─┬ express@4.16.2 
      ├─┬ accepts@1.3.4 
      │ ├─┬ mime-types@2.1.17 
      │ │ └── mime-db@1.30.0 
      │ └── negotiator@0.6.1 
      ├── array-flatten@1.1.1 
      ├─┬ body-parser@1.18.2 
      │ ├── bytes@3.0.0 
      │ ├─┬ http-errors@1.6.2 
      │ │ ├── inherits@2.0.3 
      │ │ └── setprototypeof@1.0.3 
      │ ├── iconv-lite@0.4.19 
      │ └── raw-body@2.3.2 
      ├── content-disposition@0.5.2 
      ├── content-type@1.0.4 
      ├── cookie@0.3.1 
      ├── cookie-signature@1.0.6 
      ├─┬ debug@2.6.9 
      │ └── ms@2.0.0 
      ├── depd@1.1.1 
      ├── encodeurl@1.0.1 
      ├── escape-html@1.0.3 
      ├── etag@1.8.1 
      ├─┬ finalhandler@1.1.0 
      │ └── unpipe@1.0.0 
      ├── fresh@0.5.2 
      ├── merge-descriptors@1.0.1 
      ├── methods@1.1.2 
      ├─┬ on-finished@2.3.0 
      │ └── ee-first@1.1.1 
      ├── parseurl@1.3.2 
      ├── path-to-regexp@0.1.7 
      ├─┬ proxy-addr@2.0.2 
      │ ├── forwarded@0.1.2 
      │ └── ipaddr.js@1.5.2 
      ├── qs@6.5.1 
      ├── range-parser@1.2.0 
      ├── safe-buffer@5.1.1 
      ├─┬ send@0.16.1 
      │ ├── destroy@1.0.4 
      │ └── mime@1.4.1 
      ├── serve-static@1.13.1 
      ├── setprototypeof@1.1.0 
      ├── statuses@1.3.1 
      ├─┬ type-is@1.6.15 
      │ └── media-typer@0.3.0 
      ├── utils-merge@1.0.1 
      └── vary@1.1.2 
    npm WARN getting-started@1.0.0 No repository field.
    seojaeryong:getting-started seo$ cat package.json 
    {
      "name": "getting-started",
      "version": "1.0.0",
      "description": "study project",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "simsi6",
      "license": "ISC",
      "dependencies": {
        "express": "^4.16.2"
      }
    }
    seojaeryong:getting-started seo$ 

    package.json파일 안에 금방 설치한 express 모듈의 정보가 추가 된 것을 볼 수 있습니다.

    설치된 모듈은 node-modules디렉터리 아래에 저장됩니다.

    seojaeryong:getting-started seo$ ls -al
    total 8
    drwxr-xr-x   4 seo  staff   136 10 14 18:30 .
    drwxr-xr-x   5 seo  staff   170 10 14 18:16 ..
    drwxr-xr-x  50 seo  staff  1700 10 14 18:30 node_modules
    -rw-r--r--   1 seo  staff   280 10 14 18:30 package.json
    seojaeryong:getting-started seo$ 


    이렇게 프로젝트의 초기설정은 모두 끝이났습니다.
    다음글에서 간단한 프로그램을 작성하고 실행하는 과정을 알아보겠습니다.

     

    Node.js 프로젝트 시작하기 (2/2) - 프로그램 작성 및 실행

    이전글에서 npm을 이용하여 node.js 프로젝트를 초기화 하는방법을 알아습니다. 이번글에서는 간단한 node.js프로그램을 작성하여 실행하는 법을 알아보겠습니다. 웹서버를 띄우는 간단한 예제 프로그램을 작성해..

    simsi6.tistory.com

     

    facebook twitter googleplus kakaostory naver