Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sholladay opened this issue Jul 24, 2024 · 0 comments
Labels
feature New functionality or improvement

Comments

@sholladay
Copy link
Contributor

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.

@sholladay sholladay added the feature New functionality or improvement label Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

1 participant