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

node --experimental-modules does not allow running files without an extension #16605

Closed
giltayar opened this issue Oct 30, 2017 · 7 comments
Closed
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.

Comments

@giltayar
Copy link
Contributor

  • Version: v8.8.0
  • Platform: macOS
  • Subsystem: ES Modules

Assuming the file ./node_modules/.bin/mocha exists (without a file extension!), this will work:

node ./node_modules/.bin/mocha

But this won't:

node --experimental-modules ./node_modules/.bin/mocha

It fails with ERR_UNKNOWN_FILE_EXTENSION. Which makes sense, given that it doesn't know whether it's a cjs or esm file.

But unfortunately, once --experimental-modules is not an experiment anymore, this will be a regression bug, and all cases where files without extensions are run will fail. This is unfortunate, because a lot of people (including me!) use this "trick" of not having an extension to make it a Unix executable by adding a hash bang at the beginning, e.g.: #!/usr/bin/env node.

Not sure how to resolve this, but I'm assuming that for reasons of backward compatibility, once we are out of the experiment, then this MUST work and resolve to CJS. I see two options going forward:

  1. Add a --module option to node to "tell" Node that the main module is an es module and not a CJS module.
  2. Any file that is run via cli and does not have an mjs extension is always a CJS module. This will force people in the future to await import the "real" module.

While I prefer option #1 when the ESM dust settles in a few years, for the present I would prefer option #2 given that its an exception to the rule "MJS" if and only if "ESM", and because the rules surrounding ESM are not yet intuitive to everybody.

This bug is somewhat a companion bug to #16476.

@targos
Copy link
Member

targos commented Oct 30, 2017

Related: nodejs/node-eps#62

@mscdex mscdex added the esm Issues and PRs related to the ECMAScript Modules implementation. label Oct 30, 2017
@Trott
Copy link
Member

Trott commented Nov 11, 2018

I see this bug in 8.x, but it seems to be fixed in 10.x and 11.x.

@devsnek
Copy link
Member

devsnek commented Nov 11, 2018

definitely safe to close at this point

@devsnek devsnek closed this as completed Nov 11, 2018
@Grief
Copy link

Grief commented Mar 4, 2020

Have the same issue with 13.9 while it was ok in 13.8!

@jeffs
Copy link

jeffs commented May 5, 2020

I'm having this problem with v14.2.0, even without passing --experimental-modules (which is on by default in v14), even though node v12 works fine. This is a severe regression. Please re-open.

@guybedford
Copy link
Contributor

@jeffs can you please post a new issue with a replication?

@jeffs
Copy link

jeffs commented May 5, 2020

@guybedford #33259

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

8 participants