Skip to content

Modules doc direct execution test invalid under "type": "module" #34882

Description

@tshinnic

Trying to emulate the Python __main__ check for testing purposes, I found the description in
"Accessing the Main Module" is unhelpful when package.json is set to ES modules mode "type": "module".

Suggesting if (require.main === module) is unhelpful when both require and module are undefined and node throws errors.

I see at "No require, ..." that it says no require variable is created in ESM mode, but it doesn't say module disappears also.

I see old issues #21143 and linked #21150 but don't understand if they were asking for require/module in ESM mode.

The modules doc needs updating to reflect what happens under "type": "module" mode. That text has been left behind by the node.js support for ESM.

If there is elsewhere - or could be - hints as to how to emulate the Python __main__ check that'd be of interest to more than a few people.

Activity

  1. guybedford commented on Aug 23, 2020

    @guybedford
    Contributor

    I believe the pattern to use here is path.resolve(process.argv[1]) === url.fileURLToPath(import.meta.url). It could be worthwhile discussing this.

    We did discuss import.meta.main previously, and that discussion could be reopened to follow Deno here. I'd personally support that.

  2. devsnek commented on Aug 27, 2020

    @devsnek
    Member

    You can also use separate files for your library and cli logic.

  3. added
    esmIssues and PRs related to the ECMAScript Modules implementation.
    on Dec 27, 2020
  4. github-actions commented on Jun 27, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 210 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  5. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jun 27, 2026
  6. github-actions commented on Jul 28, 2026

    @github-actions
    Contributor

    This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 120 days).
    If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions