모듈과 exports
CommonJS와 ES 모듈 방식으로 모듈을 정의하고 `module.exports`/`export`를 관리하는 패턴을 정리합니다.
// math.js — CommonJS 모듈 정의
function add(a, b) { return a + b; }
function multiply(a, b) { return a * b; }
module.exports = { add, multiply };
// index.js — 불러오기 (같은 파일을 두 번 require해도 캐시에서 반환)
const math1 = require("./math");
const math2 = require("./math"); // 동일한 객체 참조
console.log(math1 === math2); // true
console.log(math1.add(2, 3)); // 5