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 · 3 comments

Comments

Projects
None yet
5 participants
@giltayar
Copy link
Contributor

giltayar commented Oct 30, 2017

  • 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

This comment has been minimized.

Copy link
Member

targos commented Oct 30, 2017

@mscdex mscdex added the ES Modules label Oct 30, 2017

@Trott

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

devsnek commented Nov 11, 2018

definitely safe to close at this point

@devsnek devsnek closed this Nov 11, 2018

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