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

Consider to add .js extenstion to main entry point (bin/mocha) for better ES modules compatibility #4267

Closed
evg656e opened this issue May 3, 2020 · 4 comments
Labels
type: feature enhancement proposal

Comments

@evg656e
Copy link

evg656e commented May 3, 2020

Is your feature request related to a problem or a nice-to-have?? Please describe.

Since extensionless files in ESM context was dropped in latest node versions, there is a problem problem using the Mocha with new esm-loaders like ts-node esm-loader, e.g.:

node --loader ts-node/esm.mjs node_modules/mocha/bin/mocha --extension ts

causes error now:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension "" for node_modules/mocha/bin/mocha

Describe the solution you'd like
Rename bin/mocha to bin/mocha.js and add new bin/mocha which simply requires new bin/mocha.js (e.g. require('./mocha.js');) for backward compatibility.

Describe alternatives you've considered
n/a

Additional context
nodejs/node#31415
nodejs/node#33223 (comment)

@evg656e evg656e added the type: feature enhancement proposal label May 3, 2020
@cspotcode
Copy link
Contributor

FYI I posted on nodejs/node#33223 that I actually believe this is a bug on node's side. mocha declares itself to be a commonjs module, not ESM, so node should not be attempting to load it as ESM.

@boneskull
Copy link
Contributor

bin/mocha is not the main entry point; it's an executable. You should not be requiring or importing it.

See #4298 for further discussion.

@cspotcode
Copy link
Contributor

@boneskull To clarify, the use-case is invoking the executable while passing --loader to node, not to require or import the main entry point. Makes more sense when the example uses _mocha instead of mocha; not sure if _mocha has been deprecated.

# Fails due to the bug I filed in node's issue tracker
node --loader ts-node/esm node_modules/mocha/bin/_mocha --extension ts

# Allowing mocha to pass the --loader flag to node (this works)
mocha --loader=ts-node/esm --extension ts

# Omitting the equal sign breaks, even though node supports this.  Worth noting.
mocha --loader ts-node/esm --extension ts

@boneskull
Copy link
Contributor

Ahh, yes, thanks.

The recommended way would be to use mocha instead of node; it will spawn a node process, running lib/cli/cli.js with the non-Mocha flags passed to node.

_mocha is just a stub for lib/cli/cli.js, but it’s still there for backwards-compat.

we should open an issue to look into the = thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature enhancement proposal
Projects
None yet
Development

No branches or pull requests

3 participants