- 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:
- Add a
--module option to node to "tell" Node that the main module is an es module and not a CJS module.
- 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.