Skip to content

Fully support ESM helpers in projects using "type": "module" in package.json #219

@sholladay

Description

@sholladay

Runtime

node.js

Runtime version

22.5.1

Module version

7.0.3

Used with

hapi

Any other relevant information

PR #146 added partial support for ESM helpers, such as those using the .mjs extension. However, the loader still uses require(), which is only allowed to load ESM under limited circumstances. Notably, if a project using vision has "type": "module" in its package.json and has helpers with a .js file extension, then require() is not allowed to load them and Node will throw an ERR_REQUIRE_ESM error at:

const required = require(file);

I believe the only way to fix this is to use import() instead of require(). In recent versions of Node, import() is available within CommonJS modules and import() is capable of loading both ESM and CommonJS.

Since import() is async, it would have to be awaited, which could be problematic for the surrounding code, as it seems to be synchronous. But in principle, this should be a simple change.

What problem are you trying to solve?

I am unable to use helpers with ES module syntax and a .js file extension in a project with "type": "modele" in package.json.

Do you have a new or modified API suggestion to solve the problem?

No API changes should be necessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew functionality or improvement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions