[Node.js] npm(Node Package Manager)

노성준·2022년 6월 30일
0

Node.js

목록 보기
1/2

npm


npm은 Node Package Manager의 약자로, 노드의 패키지 매니저이다.
npm을 통해 Node.js의 패키지들을 관리할 수 있고, 특정기능을 구현한 패키지를 설치할 수 있다.

package.json

서비스에 필요한 패키지를 하나씩 추가하다 보면 사용을 하는 패키지의 수가 많아질 것이고, 개발 시 사용한 패키지들을 다른 환경에서도 동일하게 사용할 수 있도록 같은 버전으로 설치해 주어야 할 것이다. 이때 설치한 패키지의 버전을 관리하는 파일이 바로 package.json이다.

따라서 노드 프로젝트를 시작하기 전에는 반드시 package.json부터 만들고 시작해야 한다.

package.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: (npmtest) npmtest
version: (1.0.0) 0.0.1
description: hello package.json
entry point: (index.js) SeongJun
test command: npm WARN init canceled
PS D:\Node.js\npmtest> 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: (npmtest) npmtest
version: (1.0.0) 0.0.1                                                                                                 
description: hello package.json                                                                                        
entry point: (index.js) index.js
test command:                                                                                                          
git repository:                                                                                                        
keywords:                                                                                                              
author: author
license: (ISC)                                                                                                         
About to write to D:\Node.js\npmtest\package.json:

{
  "name": "npmtest",
  "version": "0.0.1",
  "description": "hello package.json",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "author",
  "license": "ISC"
}


Is this OK? (yes) yes

package name : 프로젝트의 이름
version : 패키지 버전
entry point : 자바스크립트 실행 파일 진입점
test command : 코드를 테스트할 때 입력할 명령어
git respository : 코드를 저장해둔 깃(Git) 저장소 주소
keywords : 프로젝트를 검색할 때 참조되는 키워드
author : 프로젝트 작성자의 정보
license : 해당 패키지의 라이선스

정보를 입력한후 yes를 입력하면
프로젝트 내부에 package.json파일이 생성된 것을 확인할 수 있다.

package.json 파일을 열어보면 다음과 같다.

{
  "name": "npmtest",
  "version": "0.0.1",
  "description": "hello package.json",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "author",
  "license": "ISC"
  "dependencies": {
    "express": "^4.18.1"
}

scripts 부분은 npm명령어를 저장해 두는 부분이다.

npm rum [스크립트 명령어]

콘솔에 다음과 같이 입력하면 해당 스크립트가 실행된다.
start나 test같은 스크립트는 run을 생략하여도 사용가능 하다.

dependencies 부분은 설치된 패키지의 이름과 설치된 버전이 저장되는 부분이다.
express패키지 4.18.1버전이 설치되 있음을 확인할 수 있다.

0개의 댓글