Visitar URL original
Consider to add .js extenstion to main entry point (bin/mocha) for better ES modules compatibility · Issue #4267 · mochajs/mocha · GitHub
Skip to content

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

Closed

Description

@evg656e

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)

Activity

  1. cspotcode commented on May 4, 2020

    @cspotcode
    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.

  2. boneskull commented on Jun 10, 2020

    @boneskull
    Member

    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.

  3. cspotcode commented on Jun 10, 2020

    @cspotcode
    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
    
  4. boneskull commented on Jun 10, 2020

    @boneskull
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions