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

Nodejs ConsoleArgument '--experimental-vm-modules' invalid #377

Open
aiselp opened this issue Aug 3, 2024 · 7 comments
Open

Nodejs ConsoleArgument '--experimental-vm-modules' invalid #377

aiselp opened this issue Aug 3, 2024 · 7 comments

Comments

@aiselp
Copy link

aiselp commented Aug 3, 2024

I plan to write a loader using the node:vm module to fully control module parsing and loading, but I found that the value of SourceTextModule is undefined, and I tried using the --experimental-vm-modules argument but it didn't work.

val runtimeOption = NodeRuntimeOptions().apply {
            setConsoleArguments(arrayOf("--experimental-vm-modules"))
        }
        V8Host.getNodeInstance().createV8Runtime<NodeRuntime>(runtimeOption).use { runtime ->
            val require = runtime.getNodeModule(NodeModuleModule::class.java).moduleObject
                .invoke<V8Value>(FUNCTION_CREATE_REQUIRE, "/")
            runtime.globalObject.set("require", require)
            val r = runtime.getExecutor(
                """
                 const { SourceTextModule } = require('node:vm');
                 SourceTextModule === undefined
            """.trimIndent()
            ).executeBoolean()
            assert(r == false)
        }

I don't know if the way to pass in the argument is correct.

@caoccao
Copy link
Owner

caoccao commented Aug 3, 2024

Some of the arguments may not work by design. Why do you create a new require?

@aiselp
Copy link
Author

aiselp commented Aug 3, 2024

Even removing the runtime.globalObject.set("require", require) doesn't work

@aiselp
Copy link
Author

aiselp commented Aug 3, 2024

In new vm.SourceTextModule(code[, options]),
options.initializeImportMeta supports passing in a function to set Import.meta. I need to set Import.meta only through this method

@caoccao
Copy link
Owner

caoccao commented Aug 3, 2024

ESM is handled by Javet module resolver. I don't think the whole thing would work.

@aiselp
Copy link
Author

aiselp commented Aug 3, 2024

I think this is feasible

const { Script, constants } = require('node:vm');
const script = new Script(
  'import("./module.mjs")',
  { importModuleDynamically: constants.USE_MAIN_CONTEXT_DEFAULT_LOADER });
script.runInNewContext()

In this code, import() is completely handled by nodejs. If module.mjs contains the import keyword, its parsing is also handled by nodejs.

@aiselp
Copy link
Author

aiselp commented Aug 3, 2024

This shows that the nodejs esm parser is still valid

@caoccao
Copy link
Owner

caoccao commented Aug 3, 2024

import() is completely implemented in JS. It has nothing to do with ESM. It's called dynamic import.

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

2 participants