Node.js: checking if an ESM module is “main”

1 · Axel Rauschmayer · July 7, 2022, 1:41 p.m.
An ESM module can be used in two ways: It can be used as a library from which other modules can import values. It can be used as script that we run via Node.js – e.g., from a command line. In that case, it is called the main module. If we want a module to be used in both ways, we need a way to check if the current module is the main module because only then do we execute the script functionality. In this blog post, we learn how to perform that check. [Read rest of post]...