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

Module Enhancement #370

Open
aiselp opened this issue Jul 18, 2024 · 3 comments
Open

Module Enhancement #370

aiselp opened this issue Jul 18, 2024 · 3 comments
Labels
help wanted Extra attention is needed

Comments

@aiselp
Copy link

aiselp commented Jul 18, 2024

When a module is loaded I want to set something on import.meta in the module for the module to use, which by default is just an empty object.
The module resolver is set for NodeRuntime, but the import() function does not seem to work. When called, it still goes through the nodejs module callback and throws an error like the following

TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
    at importModuleDynamicallyCallback (node:internal/modules/esm/utils:228:9)

When using import with, such as import fooData from './foo.json' with { type: 'json' }; , it will also be called back by nodejs and imported correctly

@caoccao
Copy link
Owner

caoccao commented Jul 18, 2024

Please leave the complete code.

@aiselp
Copy link
Author

aiselp commented Jul 18, 2024

V8Host.getNodeInstance().createV8Runtime<NodeRuntime>().use { runtime ->
            runtime.v8ModuleResolver = object : JavetBuiltInModuleResolver() {
                override fun resolve(
                    v8Runtime: V8Runtime?,
                    resourceName: String?,
                    v8ModuleReferrer: IV8Module?
                ): IV8Module? {
                    println("resolve: $resourceName referrer: ${v8ModuleReferrer?.resourceName}")
                    return super.resolve(v8Runtime, resourceName, v8ModuleReferrer)
                }
            }
            runtime.getExecutor("""
                await import("a")
            """.trimIndent()).setModule(true).executeVoid()
        }

image

@caoccao
Copy link
Owner

caoccao commented Jul 19, 2024

This is a typical pitfall in Node.js. Please check this doc out. Sample test code is at here.

@caoccao caoccao added the help wanted Extra attention is needed label Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants