Node.js
[Node.js] 내장 객체 _ exports,
개발도사(진)
2023. 2. 7. 17:59
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를 건드리지 않고 객체를 수정하는 데 쓰인다.