node icon indicating copy to clipboard operation
node copied to clipboard

esm: implement import.meta.main

Open aduh95 opened this issue 4 years ago • 42 comments

Boolean value to check if a module is run as entry point of the current process.

ES module authors need a way to determine if their code is run as CLI or as a dependency. nodejs/node#49440 have some proposal, I am implementing import.meta.main because:

  • it is arguably similar to the CJS pattern:
- if (module === require.main) {
+ if (import.meta.main) {
  • Deno has already implemented it, if we don't differ that is certainly easier for users to write code that runs everywhere.
  • It was suggested originally in the import.meta proposal.

Fixes: https://github.com/nodejs/node/issues/49440

Checklist
  • [x] make -j4 test (UNIX), or vcbuild test (Windows) passes
  • [x] tests and/or benchmarks are included
  • [x] documentation is changed or added
  • [x] commit message follows commit guidelines

aduh95 avatar Mar 12 '20 11:03 aduh95