Skip to content

Provide an equivalent of require.main for ESM #57616

Closed as duplicate of#57226
Closed as duplicate of#57226
@mcollina

Description

@mcollina

Currently there is no equivalent of require.main for ESM scripts.
Note that this is not listed at all in https://nodejs.org/api/esm.html#differences-between-es-modules-and-commonjs either.

The "known" equivalent would be to use process.argv[1] === new URL(import.meta.url).pathname), but it doesn't work with symbolic links (e.g. npm bin scripts).

A solution is to use https://www.npmjs.com/package/es-main, which practically goes down to require to implement it. I think we should really include something out of the box.

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    • Status

      Awaiting Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions