Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tshinnic opened this issue Aug 22, 2020 · 2 comments
Open

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

tshinnic opened this issue Aug 22, 2020 · 2 comments
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.

Comments

@tshinnic
Copy link
Contributor

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.

@guybedford
Copy link
Contributor

guybedford commented Aug 23, 2020

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.

@devsnek
Copy link
Member

devsnek commented Aug 27, 2020

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

@targos targos added the esm Issues and PRs related to the ECMAScript Modules implementation. label Dec 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.
Projects
None yet
Development

No branches or pull requests

4 participants