본문 바로가기

IT/공유

[공유] module.exports, exports

https://poiemaweb.com/nodejs-module

 

Node.js module | PoiemaWeb

Node.js는 사실상 모듈 시스템의 사실상 표준(de facto standard)인 CommonJS를 채택하였고 현재는 독자적인 진화를 거쳐 CommonJS 사양과 100% 동일하지는 않지만 기본적으로 CommonJS 방식을 따르고 있다. Node.js는 module 단위로 각 기능을 분할할 수 있다. module은 파일과 1대1의 대응 관계를 가지며 하나의 모듈은 자신만의 독립적인 실행 영역(Scope)를 가지게 된다. 따라서 클라이언트 사이드 Jav

poiemaweb.com

잘 정리된 것 같다.

module.exports 의 경우 기본적으로 한 개의 모듈만 내보낸다고 보면 된다.

hi.js
module.exports = () => {
	return {
    	ang() { return 'ang' },
        angli() { return 'angli' }
    };
}

app.js
const hi = require('./hi');

console.log(hi().ang());
console.log(hi().angli();

 

hi.js
exports.ang = () => {
  console.log('hi');
}

exports.angli = 'angli';

app.js
const hi = require('./hi');

hi.ang();
console.log(hi.angli);

 

'IT > 공유' 카테고리의 다른 글

[공유] https. ubuntu apache2 openssl 적용  (0) 2019.10.16
[공유] React Code Splitting  (0) 2019.10.15
[공유] firebase RealtimeDatabase vs Cloud Filestore  (0) 2019.10.11
[공유] 쿠버네티스, 도커  (0) 2019.10.08
[공유] firebase  (0) 2019.10.02