Skip to content

How to determine whether a file has been run directly inside ES module #2065

@sosoba

Description

@sosoba
  • Node.js Version:
    12.6.0
  • OS:
    Windows
  • Scope (install, code, runtime, meta, other?):
    runtime, documentation

Current documentation says:

  1. Accessing the main module:

When a file is run directly from Node.js, require.main is set to its module. That means that it is possible to determine whether a file has been run directly by testing require.main === module.

  1. Differences Between ES Modules and CommonJS:

These CommonJS variables are not available in ES modules.

The new challege is - how to determine whether a file has been run directly inside ES module?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions