본문 바로가기
Node.js

[Node.js] 내장 객체 _ exports,

by 개발도사(진) 2023. 2. 7.

exports 객체

앞서 모듈을 만드는 기능을 연습했을 때는 module.exports 를 사용했다.

/*
const toExport_1 = 1997;
const toExport_2 = 2002;


module.exports = {
    toExport_1,
    toExport_2,
};
*/

exports.toExport_1 = 1997;
exports.toExport_2 = 2002;

주석처리한 부분과 맨 아래 두 줄은 

const { toExport_1, toExport_2 } = require('./exportTest');

function importSum() {
    return toExport_1 + toExport_2;
}

console.log(importSum());

로 실행시켰을 때 결과가 같다.

 

module.exports와 exports는 같은 객체를 참조한다. (exports가 module.exports를 참조). 

 

require는 항상 module.exports를 참조하므로, exports를 이용해 module.exports를 건드리지 않고 객체를 수정하는 데 쓰인다.

 

'Node.js' 카테고리의 다른 글

[Node.js] Server, Client  (0) 2023.02.06
[Node.js] REPL, Module, 내장 객체_1  (0) 2023.02.03
[Node.js] 기본_1  (0) 2023.02.01