Skip to content

Bug/Feature Request: Better Error for Modules Requiring File Extension #38484

@merlinstardust

Description

@merlinstardust
  • Version: v16.0.0
  • Platform: Mac
  • Subsystem:

What steps will reproduce the bug?

  1. Start a new project with "type": "module" in package.json
  2. Create two files (fileA.js and fileB.js)
  3. Relative import one file into the other (import fileB from './fileB';)
  4. Run the file with the import (node fileA.js)

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

Import without a file extension or the flag

What is the expected behavior?

Gives a vague error about not finding the module

What do you see instead?

Give a more specific error that file extensions need to be added in imports (import fileB from './fileB.js'; and/or that node should be run with --experimental-specifier-resolution=node

Additional Info

I'm asking for this because I spent the last two hours trying to debug the below errors in my project.

Any documentation/tutorials should be updated with a strongly emphasized note about this as well as it is not obvious at all.

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/merlin/code/inclusion-bot/index.js' imported from /Users/merlin/code/inclusion-bot/
    at new NodeError (node:internal/errors:363:5)
    at finalizeResolution (node:internal/modules/esm/resolve:296:11)
    at moduleResolve (node:internal/modules/esm/resolve:742:10)
    at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:853:11)
    at Loader.resolve (node:internal/modules/esm/loader:89:40)
    at Loader.getModuleJob (node:internal/modules/esm/loader:242:28)
    at Loader.import (node:internal/modules/esm/loader:177:28)
    at node:internal/modules/run_main:50:28
    at Object.loadESM (node:internal/process/esm_loader:68:11) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/merlin/code/ib2/events' imported from /Users/merlin/code/ib2/index.js
Did you mean to import ../events.js?
    at new NodeError (node:internal/errors:363:5)
    at finalizeResolution (node:internal/modules/esm/resolve:307:11)
    at moduleResolve (node:internal/modules/esm/resolve:742:10)
    at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:853:11)
    at Loader.resolve (node:internal/modules/esm/loader:89:40)
    at Loader.getModuleJob (node:internal/modules/esm/loader:242:28)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:73:40)
    at link (node:internal/modules/esm/module_job:72:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.moduleIssues and PRs related to the module subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions