Main function in Node.js
Sever/Node.js 2021. 12. 13. 21:14대부분의 다른 프로그래밍 언어나 런타임 환경과 달리, Node.js는 프로그램의 진입점을 지정하는 특별한 주 함수가 내장되어 있지 않다.
그러나 Node.js에서 이를 달성할 수 있는 쉬운 방법이 있다
이 패턴은 현재 모듈이 메인 모듈인지 확인하는 것입니다.
required.main === module.
이 경우 다른 파일이 가져오는 것이 아니라 현재 파일이 직접 실행된 것이며, 이 경우 주 함수를 호출할 수 있습니다.
// main.js / main.ts (the filename doesn't matter)
function main() {
console.log("Hello world");
}
if (require.main === module) {
main();
}
참고
https://nodejs.org/api/modules.html#modules_accessing_the_main_module
https://m.blog.naver.com/jdub7138/221022257248