The knowledge I’m missing is how does this package know to point to my brew installation
It doesn’t. The
@types/k6 package contains the TypeScript type definitions for a specific k6 version. It’s up to you to ensure that the k6 (or xk6-browser) version you’re running actually matches the definitions in
@types/k6 doesn’t include any of the xk6-browser APIs, so it’s only helpful for k6 APIs.
The binary is accessible globally, but how to import into exactly the same template project linked above is not clear.
You don’t import the k6/xk6-browser binary anywhere. Your JS/TS project is not aware in anyway of the k6/xk6-browser version you’ll eventually run the script with.
Despite the fact that you said the @types library in NPM has no relation to k6 itself, the template example points all the imports to exactly the @types definitions.
Right, the template project points to a specific
@types/k6 version that the resulting script is supposed to be run with. This is merely useful during development so that you get the same type definitions for the k6 APIs of the version you’re targetting, but it has no relevance when you actually run the script.
So how does it know where the real binary is, and how can I import functionality from xk6-browser?
It doesn’t know. When you run
k6 run ... or
xk6-browser run ..., the binary will be chosen according to your
$PATH, and any imported libraries will be resolved at runtime. In the case of imports from
k6/x/browser, this will only work with the
xk6-browser binary, since that binary actually has the JS module exposed by the xk6-browser extension, which is built into the binary itself. This is why the plain
k6 binary doesn’t work for running xk6-browser scripts, and you need to use a separate binary.
In your case, since you installed k6 via Brew, you can’t just run
k6 run <xk6-browser script>, since that k6 binary doesn’t have the
k6/x/browser module. So you’ll need to run
xk6-browser run <xk6-browser script> instead. Note that the reverse is possible: you can run plain k6 scripts with the
xk6-browser binary, but not the other way around.
I hope this clears things up.