8. Common js 란 무엇인가

https://yceffort.kr/2023/05/what-is-commonjs#서론
https://yceffort.kr/2020/08/commonjs-esmodules

CommonJs 란

모듈이란

const { PI } = Math 
exports.sum = (a, b) => a + b 
exports.circumference = (r) => 2 * PI * r

module.exports


module.exports = class Square 
	{ constructor(width){ this.width = width } 
	area() 
	{ return this.width ** 2 } }

node.js는 언제 commonjs를 사용할까?

Module Wrapper

;(function (exports, require, module, __filename, __dirname) { // 내부 모듈 코드는 실제로 여기에 들어감 })

순환참조

동기적으로 실행됨

트리쉐이킹이 되지 않는다

하지만..모든 클로져를 합치는 것은 좋지 않다.

// test.js 
module.exports = { [globalThis.hello]: 'world', }


// index.js

const hello = 'hello'
globalThis[hello] = hello
const test = require('./test.js') 
console.log(test[hello]) // 'world'

ESM 과 CJS 의 차이점 간략 요약