In k6 github repo, we found we can import js modules as below codes:
out, err := vm.RunString(
`
var m = require("./math.js");
m.add(1,2);
`
)
But k6 also can do it like this:
out, err := vm.RunString(
`
import {add} from 'k6/x/math';
add(1,2);
`
)
I found the codes where k6 module registering modules as below (Register method just load the module script to goja runtime and put it into a modules array),
modules.Register("k6/x/math", new(math.RootModule))
The question is where are k6 codes to support import a path like "import {add} from 'k6/x/math'"? Thanks.
You'd need to define the
exports
within your module code. As an example, take a look at theExports()
function in theencoding
module.Hope this helps!