Skip to content

Extenstionless main files and ES modules interactionΒ #33223

@evg656e

Description

@evg656e
  • Version: 13.9, 14.1
  • Platform: All
  • Subsystem: ES modules

What steps will reproduce the bug?

Create file package.json:

{
    "type": "module"
}

Create file main (no extension):

console.log('ok');

Run:

node ./main

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior?

main file to be executed.

What do you see instead?

> node ./main

internal/modules/run_main.js:54
    internalBinding('errors').triggerUncaughtException(
                              ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension "" for C:\Users\evg656e\Projects\sandbox\js\experimental-loader\main
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
    at Loader.getFormat (internal/modules/esm/loader.js:113:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:244:31)
    at async Loader.import (internal/modules/esm/loader.js:178:17) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Additional information

This works in version 12. But does not work in versions 13 and 14. --experimental-specifier-resolution=node flag does not help.

Real world example: we have mocha (which main entry file is extensionless), we have ts-node esm-loader, now we want to test ts files with native es modules, like that:

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

but we get the error described above. But if we rename node_modules/mocha/bin/mocha to node_modules/mocha/bin/mocha.js, then everything works as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions