Skip to content

Improve the error messages instead of the generic "Error: Cannot find module 'xyz'" #5758

@drywolf

Description

@drywolf
  • Version: 4.2.6
  • Platform: Windows 10 64-bit
  • Subsystem: module.js

Hi,

I just had a case where node reported one of my local modules (referenced by "my-module": "file:../my-module") as missing with the default generic node error:

module.js:328
    throw err;
    ^

Error: Cannot find module 'my-module'
    at Function.Module._resolveFilename (module.js:326:15)
    at Function.Module._load (module.js:277:25)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\code\my-service\server\server.js:3:23)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:442:10)

When I looked in the node_modules folder the my-module subdirectory was there. From that point on I spent about 30 min to an hour to figure out why node didn't recognize the module that was clearly there.

In the end it turned out that the entry file for the my-module (specified in package.json "main") was missing inside the node_modules\my-module folder. So the actual fix took about 15 seconds but the time it took to figure out what npm / node where trying to tell me about the error was way too long.
(for sure I will look immediately for the main file next time, but I'm raising this issue to help others not waste time on the same thing)

My suggestion would be to add additional checks to see if the "main" file for a package is really available at the configured path from package.json (probably here).

So if the file is missing the node should produce a more helpful error in the future.

Please let me know what you think about this, once I have some feedback I might work on PR for this.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.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