hapijs / hapijs/vision

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

Open
#219 0 comments 0 reactions 0 assignees View on GitHub
feature
Dominant language
JavaScript
Stars
198
Forks
68
PR merge metrics
No merged PRs in 30d

Description

### 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: https://github.com/hapijs/vision/blob/cc598dba5ac049d183138155ad15292153859623/lib/manager.js#L210

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.