detect whether ES Module is run from command line in Node
Use if (import.meta.url === `file://${process.argv[1]}`) { // module was not imported but called directly } See the MDN docs on import.meta for details. Update Sep 27, 2021 Perhaps more robust, but involving an extra import (via Rich Harris) import { pathToFileURL } from ‘url’ if (import.meta.url === pathToFileURL(process.argv[1]).href) { // module was not imported but … Read more