Using proxy for getting remote HTTP(S) modules?

Hi. My test uses remote modules like this:

import { randomItem } from "https://jslib.k6.io/k6-utils/1.2.0/index.js";
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";

It works fine locally, but fails on CI with the following errors:

time="2022-04-25T12:44:36Z" level=error msg="Module specifier \"https://jslib.k6.io/k6-utils/1.2.0/index.js\" was tried to be loaded as remote module by prepending \"https://\" to it, which didn't work. If you are trying to import a nodejs module, this is not supported as k6 is _not_ nodejs based. Please read https://k6.io/docs/using-k6/modules for more information. Remote resolution error: \"Get \"https://jslib.k6.io/k6-utils/1.2.0/index.js\": dial tcp xxx.xxx.xxx.xxx:443: i/o timeout\"\n\tat go.k6.io/k6/js.(*InitContext).Require-fm (native)\n\tat file:///workspace/1/s/k6/assignees-filter-test.js:4:0(60)\n" hint="script exception"

I know, my CI node is behind the proxy, so an expected solution would be to set appropriate env variables (http(s)_proxy). I’ve tried them all (including uppercased ones), but k6 keeps failing with above error. I’am sure that my proxy configuration should work, because when I add proxy-related env-variables, wget -qO- https://jslib.k6.io/k6-utils/1.2.0/index.js starts working (but nor k6, unfortunately).

What options do I have to resolve this issue? How to teach k6 to use proxy for resolving http(s) modules?

Which k6 version are you using? Setting https_proxy=IP:port or HTTPS_PROXY=IP:port seems to work on my machine with the latest k6 version :confused: Mind you, that will also make k6 proxy all HTTP requests from the script thought the given proxy, but it should also cause it to load any remote modules though it as well…

Hi @ned. I am using the latest version of grafana/k6 container with k6 v0.37.0 (2022-03-15T09:43:26+0000/67657f4, go1.17.8, linux/amd64) inside.

The value for proxy env variables is http://some-domain:8080. As I said, wget starts working when I provide proxy settings, but k6 gives me the above error.

How exactly are you executing the docker container? If I run this:

docker run -i --rm --env https_proxy=127.0.0.1 grafana/k6 run - <<< 'import { randomItem } from "https://jslib.k6.io/k6-utils/1.2.0/index.js"; export default function() {};'

I get this error:

time="2022-04-27T05:31:07Z" level=error msg="Module specifier \"https://jslib.k6.io/k6-utils/1.2.0/index.js\" was tried to be loaded as remote module by prepending \"https://\" to it, which didn't work. If you are trying to import a nodejs module, this is not supported as k6 is _not_ nodejs based. Please read https://k6.io/docs/using-k6/modules for more information. Remote resolution error: \"Get \"https://jslib.k6.io/k6-utils/1.2.0/index.js\": proxyconnect tcp: dial tcp 127.0.0.1:80: connect: connection refused\"\n\tat go.k6.io/k6/js.(*InitContext).Require-fm (native)\n\tat file:///-:1:0(17)\n" hint="script exception"

That’s because I don’t have a convenient HTTPs proxy to test with, but it’s at least showing that k6 is trying to use the given proxy to load the import-ed file. If I remove --env https_proxy=127.0.0.1 from the command, the script runs without an error.