npm이란
Node Package Manger
- 노드의 패키지 매니저
- 다른 사람들이 만든 소스 코드들을 모아둔 저장소
- 남의 코드를 사용하여 프로그래밍 기능
- 이미 있는 기능을 다시 구현할 필요가 없어 효율적
- 오픈 소스 생태계를 구성중
- 패키지 : npm에 업로드된 노드 모듈
- 모듈이 다른 모듈을 사용할 수 있듯 패키지도 다른 패키지를 사용할 수 있음
- 의존 관계라고 부름
package.json
현재 프로젝트에 대한 정보와 사용 중인 패키지에 대한 정보를 담은 파일
- 같은 패키지라도 버전별로 기능이 다를 수 있으므로 버전을 기록해두어야 함
- 동일한 버전을 설치하지 않으면 문제가 생길 수 있음
- 노드 프로젝트 시작 전 package.json 부터 만들고 시작함(npm init)
D:\code\node\publish>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: (publish) npmtest
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author: hd
license: (ISC) MIT
About to write to D:\code\node\publish\package.json:
{
"name": "npmtest",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "hd",
"license": "MIT",
"description": ""
}
Is this OK? (yes) yes
// npm init 완료 하면 package.json 파일이 생성됨
// package.json
{
"name": "npmtest",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "hd",
"license": "MIT",
"description": ""
}
>> npm run [스크립트명]으로 스크립트 생성
D:\code\node\publish>npm run test
> npmtest@1.0.0 test
> echo "Error: no test specified" && exit 1 //에러를 발생시키면서 프로세스를 종료
"Error: no test specified"
// 아래와 같이 설치를 하면 package.json 에 dependencies 에 버전이 표시된다.
// node_modules 디렉토리가 생성되면서 스크립트들이 생긴다.
D:\code\node\publish>npm i express
added 64 packages, and audited 65 packages in 8s
12 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
//package.json
{
"name": "npmtest",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "hd",
"license": "MIT",
"description": "",
"dependencies": {
"express": "^4.19.2"
}
}
// 아래와 같이 -D 옵션으로 설치를 하면 package.json 에 devDependencies에 버전이 표시된다.
// devDependencies는 개발할 때만 쓰이는 패키지들이다.
D:\code\node\publish>npm i -D nodemon
added 29 packages, and audited 94 packages in 6s
16 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
//package.json
"devDependencies": {
"nodemon": "^3.1.4"
}
node_modules 크기가 크기 때문에 지워서 배포를 한다.(package.json 만 가지고 있다가 설치를 한다.)
npm i -g rimraf (-g는 글로벌 설치, 글로벌 설치는 dependencies 에 기록되지 않는다.)
npm i rimraf -D
npx 를 사용하면 글로벌 설치한 것처럼 사용이 가능하다.
글로벌 설치는 최대한 기피하는 것이 좋다.
SemVer 버저닝
노드 패키지의 버전은 SermVer(유의적 버저닝)방식을 따름
- Major(주 버전), Minor(부 버전), Patch(수 버전)
- 노드에서는 배포를 할 때 항상 버전을 올려야 함
- Major는 하위 버전과 호환되지 않은 수정 사항이 생겼을 때 올림
- Minor는 하위 버전과 호환되는 수정 사항이 생겼을 때 올림
- Patch는 기능에 버그를 해결했을 때 올림
예) 1.0.7 ←- 1 : Major
←- 0 : Minor
←- 7 : Patch
버전 앞에 기호를 붙여 의미를 더함
dependencies에서 아래와 같이 고정된다는 뜻이다.
^1.0.7 : Major까지 고정
~1.0.7 : Minor까지 고정
1.0.7 : Patch까지 고정
- @latest는 최신을 의미
- @next로 가장 최신 배포판 사용 가능(불안정함)
- 알파/베타/RC 버전이 존재할 수 있음
npm i express@latest ← 최신 버전 설치
npm i express@3 ← Major 3인 버전 설치
npm i express@3.5.1 ← 3.5.1 정확한 버전 설치
npm i express@next ← 가장 최신 배포판
npm outdated : 어떤 패키지에 기능 변화가 생겼는지 알 수 있음
npm uninstall 패키지명 : 패키지 삭제 (npm rm 패키지명 으로도 가능)
npm search 패키지명 : npm 패키지를 검색할 수 있음
npm info 패키지명 : 패키지의 세부 정보 파악 가능
D:\code\node\publish>npm search express
express
Fast, unopinionated, minimalist web framework
Version 4.19.2 published 2024-03-25 by wesleytodd
Maintainers: dougwilson linusu sheplu blakeembrey ulisesgascon wesleytodd mikeal
Keywords: express framework sinatra web http rest restful router app api
https://npm.im/express
D:\code\node\publish>npm info express
express@4.19.2 | MIT | deps: 31 | versions: 276
Fast, unopinionated, minimalist web framework
http://expressjs.com/
npm publish : 자신이 만든 패키지를 배포
npm unpublish : 자신이 만든 패키지를 배포 중단(배포 후 24시간 내에만 가능)
https://docs.npmjs.com/cli/v9/commands ←명령어 바뀌는 경우 있으니 사이트 참조