
npm ( Node Package Modules )
- ํ์ฅ ๋ชจ๋(ํจํค์ง) ๊ฒ์
- ํจํค์ง ๋งค๋์
- http://www.npmjs.org
- ๋ชจ๋ ๊ฒ์
- ๋ชจ๋ ์์ธ ์ ๋ณด
- ๋ชจ๋ ์ค์น ๋ฐฉ๋ฒ
- API ์ค๋ช
, ์์
Module
- Module ์ค์น, ์ญ์ (ํ์ฅ ๋ชจ๋)
- Module ๊ฒ์
- Module ์ ๋ณด
- ํจํค์ง ์ ๋ณด ์์ฑ
์ค์นํ๋ ๋ฐฉ๋ฒ
- Nodejs๋ฅผ ์ค์นํ๋ค๋ฉด ๊ฐ์ด ์ค์น ๋์ด์๋ค.
- ํ์ธ ๋ช
๋ น์ด :
npm help
์ฃผ์ ๋ช
๋ น์ด
npm init
: ํจํค์ง ์ค๋น package.json ์์ฑ
npm install
: ํจํค์ง์ ํ์ํ ๋ชจ๋ ์ค์น
npm install package
: ๊ฐ๋ณ ํจํค์ง ์ค์น
npm list
: ์ค์น๋ ๋ชจ๋ ๋ชฉ๋ก ๋ณด๊ธฐ
npm list -g --depth=0
: Global List ํ์ธํ๊ธฐ
npm info
: ๋ชจ๋ ์ ๋ณด
npm search
: ๋ชจ๋ ๊ฒ์
npm update
: ๋ชจ๋ ์
๋ฐ์ดํธ
npm uninstall
: ๋ชจ๋ ์ญ์
์ ์ญ์ค์น
- ํ๋ฒ ์ค์น๋ก ๋ชจ๋ ํ๋ก์ ํธ์์ ์ฌ์ฉ
- ์ ํธ์ฑ(mocha,nodemon)์ ์ ์ญ ์ค์น ๊ถ์ฅ
- lib/node_modules
- ๊ด๋ฆฌ์ ๊ถํ ํ์ -g ์ต์
sudo npm install -g [Module]
sudo npm uninstall -g [Module]
์ง์ญ์ค์น
- ๋๋ถ๋ถ์ ๋ชจ๋์ ์ง์ญ ๋ชจ๋ ๊ถ์ฅ
- ํ๋ก์ ํธ๋ง๋ค ์ค์น
- node_modules
๋ชจ๋ ์ค์น
npm install [Module][@Version]
- ์์กด์ฑ ์๋ ๋ชจ๋๋ ๊ฐ์ด ์ค์นํ๋ค.
ํจํค์ง ์ ๋ณด
ํจํค์ง ์ค์ ํ์ผ
npm init
์ผ๋ก ์์ฑ
- ํจํค์ง์ ๋ํ ์ ๋ณด ์
๋ ฅ
npm install Module --save
: package.json์ ์์กด์ฑ ์ ๋ณด ๊ธฐ๋กํ๊ธฐ
npm install Module --save-dev
: ์ค๋ฌดํ๊ฒฝ์ด ์๋ ๊ฐ๋ฐํ ๋๋ง ์ฌ์ฉํ๋ค๊ณ ์๋ฆฌ๋ ์ต์
- package.json
{
"name": "npm-exercise",
"version": "1.0.0",
"description": "npm sample",
"main": "app.js",
"dependencies": {
"async": "^1.5.0",
"jade": "^1.11.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
ํ์ฅ ๋ชจ๋ : nodemon
- ์์ค์ฝ๋ ์์ ํ ์๋ ์ฌ์์. ๋งค์ฐ ํธํ๋ค.
- ์ค์น : ๊ธ๋ก๋ฒ๋ก ์ค์น
npm install -g nodemon

- ์คํ๋ฐฉ๋ฒ :
nodemon app.js

๋ชจ๋ ๋ง๋ค๊ธฐ
- ์์ค ์ฝ๋ ๋ถ๋ฆฌ
- ๋ชจ๋ ์์ฑ ๋ฐฉ๋ฒ :
module.exports
- ๋ชจ๋ ๋ก๋ฉ :
require(๊ฒฝ๋ก)
๋ชจ๋ ์์
module.exports.hello = function() {
console.log('Hello World');
}
exports.hello = function() {
//module ์๋ต ๊ฐ๋ฅ
}
- ํธ์ถํ๊ธฐ (exports๋ฅผ ํ์ง ์๋ ํจ์๋ ์ฌ์ฉ ๋ถ๊ฐ)
var greeting = require('./greeting');
greeting.hello();
ํด๋์ค exports
- ๋จ ํ๋์ ํด๋์ค exports
- class
function Exercise() {
}
Exercise.prototype.run = function() {
console.log('run!');
}
module.exports = Exercise; //module ์๋ต ๋ถ๊ฐ
var Exercise = require('./exercise.js');
var exercise = new Exercise();
exercise.run();
๊ฐ์ฒด exports
- ๋จ ํ๋์ ๊ฐ์ฒด exports
- Object
var student = {
hour : 0,
study : function(){
this.hour++;
console.log(this.hour+'์๊ฐ์งธ ๊ณต๋ถ ์ค');
}
};
module.exports = student; //module ์๋ต ๋ถ๊ฐ
var student = require('./student.js');
student.study();