Skip to content

ERR_MODULE_NOT_FOUND: add a property to exception object to hold failing import's argument #37581

Closed as not planned
@rulatir

Description

@rulatir

Is your feature request related to a problem? Please describe.

I am developing a framework that uses node as a host for user scripts. The framework loads them dynamically with the async import() function, and the userscripts can also import other modules with import statement.

When I call that import() function, I basically want to rethrow all errors thrown by it, except for one very specific condition: when the import() function fails with ERR_MODULE_NOT_FOUND because the userscript file itself wasn't present in the location just tried. The userscript might still be found in some other search path, which is why its absence in any given candidate location is not an error. If however the import() function failed with ERR_MODULE_NOT_FOUND because some other module imported by the userscript was not found, then I want to rethrow that error.

Describe the solution you'd like

Add a property to the exception object thrown for ERR_MODULE_NOT_FOUND that will hold the exact value of the argument that was given to the failing import function or statement.

Describe alternatives you've considered

I considered checking for existence first and then attempting to import(), but that is a known antipattern.

I considered parsing the message or stack property, but that would be a hack. These properties are probably locale dependent and their format is not set in stone.

I looked into custom loaders and found the subject difficult, scary, apparently indefinitely experimental, and an overkill for this particular purpose. I need a simple and reliable way.

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