Skip to content

Detect if NODE.JS script is called direct via bash (unix) / cmd (windows) or imported (ESM module loader) #2420

@mxcd

Description

@mxcd
  • Node.js Version: 13.6
  • OS: Windows 7 (RIP)

How can I detect whether my node.js file was called directly from console (windows and unix systems) or loaded using the ESM module import ( import {foo} from 'bar.js')

The question was already answered for regular node.js files (Detect if called through require or directly by command line) but since require is not available with ESM modules, I am in need of a different solution.

Please feel free to also answer this on stackoverflow for future reference:
https://stackoverflow.com/questions/59752798/detect-if-node-js-script-is-called-direct-via-bash-unix-cmd-windows-or-imp

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions