[Node, Udemy] 섹션4: 개선된 개발 워크플로우 및 디버깅
NPM 스크립트의 이해
지금까진 node "파일명"와 같이 노드를 실행했는데, 패키지가 많아졌을 때 관리하기 쉬운 매니저툴인 npm을 넣어보자.
npm은 노드를 설치하면 자동적으로 설치되어 있다. npm init을 한 번 입력해보면 패키지 pakcage.json을 만들어 준다.
npm init
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
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: (study)
version: (1.0.0)
description: Complete Node.js Guide
entry point: (app.js)
test command:
git repository: (https://github.com/somefood/node-study.git)
keywords:
author:
license: (ISC)
About to write to C:\10017011\study\node-study\package.json:
{
"name": "study",
"version": "1.0.0",
"description": "Complete Node.js Guide",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/somefood/node-study.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/somefood/node-study/issues"
},
"homepage": "https://github.com/somefood/node-study#readme"
}
Is this OK? (yes)
생성된 package.json을 보면 위에서 입력한대로 완성이 되어있다.
{
"name": "study",
"version": "1.0.0",
"description": "Complete Node.js Guide",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/somefood/node-study.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/somefood/node-study/issues"
},
"homepage": "https://github.com/somefood/node-study#readme"
}
여기서 scripts를 보면 스크립트들을 넣어서 실행할 수 있게끔 할 수 있다. 한 번 scripts 안에 다음과 같은 코드를 넣어보자.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js"
},
이후 터미널에서 npm start를 입력하면, node app.js가 실행되는 것을 볼 수 있다! 그러면 파일명이 바뀐다면 scripts의 내용만 바꿔주고 나는 npm start만 입력해주면 되는 것이다.
"start-server": "node app.js"
이것도 추가하고, npm start-server 입력해보면 에러가 난다. 지원하지 않는 명령어이기 때문이다. 만약에 이렇게 하고 실행시키고 싶으면 npm run start-server을 입력하면 된다.
제 3자 패키지 설치하기
우리는 이제 써드파티 라이브러리들을 참고할 것이고, 이 설치 관리를 npm에서 해줄 것이다. 먼저 한 번 nodemon을 설치해보자. npm install nodemon --save-dev 이렇게 하면 node_modules가 생성된 것을 볼 수 있고, package.json에 devDependencies에 nodemon을 추가됨을 볼 수 있다. node_modules에 폴더가 상당히 만들어진 것을 볼 수 있는데 의존된 것을 다 받아오기 때문이다. package-lock.json은 오늘 설치한 버전이 기록되기에, 다른 사람들과 공유할 때 이 파일을 활용해 버전을 맞출 수 있을 것이다.