CommonJS

ALTANIS·2021년 2월 24일
0

CommonJS ?
간단하게 자바스크립트 모듈화를 위한 프로젝트 중 하나라고 한다.


사용 방법

저장되어 있는 모듈을 불러올 때

require('모듈이름')

// example
const fs = require('fs')
// 파일 시스템 모듈을 사용할 때 

다른 스크립트를 불러올 때
아직 이개념은 이해가 안된다. 정리가 되면 수정하겠다.

//script1.js
const foo = require('./script2.js') // 같은 디렉토리에 있을 때


//script2.js
console.log('hello module');

모듈로 노출시킬 때

//script1.js
const foo = require('./script2.js') // 같은 디렉토리에 있을 때
console.log(foo); // 'hello module'


//script2.js
module.exports = 'hello module';

module.exports 와 exports

exports는 module.exports의 축약형이다.
둘이 비슷하지만 exports는 module.exports를 참조하고 있다.
module.exports 에 바로 값을 할당해버리면
그 값만 사용이 가능하다.

다시 정리하도록 하겠다.

module.exports = 3;

0개의 댓글