프로그래밍/Node.js

Node.js 생활코딩 정리 – 모듈과 NPM(Node Package Manager)이란?

가카리 2016. 11. 21. 20:14
반응형

모듈과 NPM(Node Package Manager)이란?

 

const http = require('http');//http가 모듈임

 

var o = require('os');//os가 모듈임

console.log(o.platform());

 

underscore 모듈 설치시(cmd창은 항상 관리자모드로 실행 필요)

 

 

npm init 를 치고

 

entry point 는 이 패키지의 첫번째 실행하는 js 파일

test command : TDD 시 테스트 명령어

git repository : 깃의 저장소 주소

 

 

 

실행 후 package.json 파일이 생성된다. 이것을 이용해서 다른 사람들이 이 패키지를 설치할 수 있게 된다.

 

 

npm install underscore를 치면

 

 

node_modules 폴더가 생긴다.

 

 

 

npm install underscore --save를 치면

 

--save를 치면 dependencies가 생겨서 이 패키지 실행

 

만약 error가 나면 npm cache clean 후 다시 해보면 설치됨

 

 

이것을 치면 dependencies에 underscore가 생김

 

package.JSON이란 파일만 있으면 언제든지 underscore 모듈을 포함 시킬 수 있다는 의미(일종의 include <stdio.h>)

 

예제 실습

underscore.js

 

var _ = require('underscore');

var arr = [3, 6, 9, 1, 12];

console.log(arr[0]);

console.log(_.first(arr));//첫번째 원소를 리턴함

console.log(arr[arr.length-1]);

console.log(_.last(arr));//위와 같은 역할을

 

 

실행 결과

 

 

출처 : 생활코딩 (https://opentutorials.org/course/2136)

반응형