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

dynamic imports throw an error in module mode #51

Open
TimDaub opened this issue Sep 25, 2024 · 1 comment
Open

dynamic imports throw an error in module mode #51

TimDaub opened this issue Sep 25, 2024 · 1 comment

Comments

@TimDaub
Copy link

TimDaub commented Sep 25, 2024

I was made aware that make-asynchronous does't import dynamic imports (https://github.com/sindresorhus/make-asynchronous/blob/7fce502f8c5f7d2a4af20f253e9dbb14c68fa21c/test.js#L68-L74). So I looked into this by testing dynamic imports in web-worker, and I found that they somehow throw an error in "module" mode. Why?

> new (await import('web-worker')).default('data:,import("make-asynchronous").then(console.log)');
Worker {}
> new (await import('web-worker')).default('data:,import("make-asynchronous").then(console.log)', {type: "module"});
Worker {}
> TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:405:5)
    at getSource (node:internal/modules/esm/load:47:13)
    at defaultLoad (node:internal/modules/esm/load:111:38)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async ModuleLoader.load (node:internal/modules/esm/loader:417:7)
    at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:288:11)
    at async link (node:internal/modules/esm/module_job:67:21) {
  input: 'data:,import("make-asynchronous").then(console.log)',
  code: 'ERR_INVALID_URL'
}

Through some debugging I got to an error involving ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING

@TimDaub
Copy link
Author

TimDaub commented Sep 25, 2024

It seems one could add importModuleDynamically: true here:

web-worker/node.js

Lines 207 to 209 in b89a392

return VM.runInThisContext(data, {
filename: 'worker.<'+(name || 'data:')+'>'
});

But even node v22 docs say that:

This option is still part of the experimental modules API. We do not recommend using it in a production environment.

Bummer, I would have loved to use this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant