Functions inaccessible to scenario/executor w Docker


I am facing an issue accessing an exported function when invoked from a executor/scenario.

Here are a few details:

File: helper-functions.js
import { check, sleep } from 'k6'; export function exportfn1 { sleep(1); }

File: main.js
import { check, sleep } from 'k6'; import { exportfn1 } from '/opt/lib/helper-functions.js'; import http from 'k6/http';

Config file:
{ "scenarios": { "scenario1": { "executor": "constant-vus", "vus": 5, "duration": "10s", "gracefulStop": "0s", "exec": "exportfn1" } } }

Docker command used to run K6:
docker run --rm -v "$(pwd)"/config.json:/etc/config/config.json:z -v "$(pwd)"/helper-functions.js:/opt/lib/helper-functions.js:z -i repo/k6:0.31.0 --config /etc/config/config.json run - < main.js

Error message:
level=error msg=“There were problems with the specified script configuration:\n\t- executor scenario1: function ‘exportfn1’ not found in exports”

I seem to, on the outset, following documentation here - but unable to figure out why the function is inaccessible.

Thanks in advance for any insight.

Hi there, welcome to the forum :slight_smile:

So this is not related to Docker, but with how you’re exporting the functions. The function you specify in "exec" should be exported by the main script, and not only by any helper scripts.

So if you change the import in main.js to: export { exportfn1 } from '/opt/lib/helper-functions.js'; or simply add export { exportfn1 }; it should run fine.

Much appreciated @imiric