Can I import helper functions in a k6 script?

I would like to create a lot of helper functions that make up my Load tests.
like

  • getToken
  • createWorksite
  • getShifts

Normally I would create a file called helpers.js and import them into the javascript I want to use them in.

When I try to do this with K6 I get this kind of error:

GoError: The moduleSpecifier "./helpers/getJwt" couldn't be found on local disk. Make sure that you've specified the right path to the file. If you're running k6 using the Docker image make sure you have mounted the local directory (-v /local/path/:/inside/docker/path) containing your script and modules so that they're accessible by k6 from inside of the container, see https://k6.io/docs/using-k6/modules#using-local-modules-with-docker. at reflect.methodValueCall (native)

I am not using docker just my local installation of k6
Any help would be appreciated.

Yes, you can have helper functions and import them. Just make sure that you include the file extension in the import statement, e.g. import getJwt from "./helpers/getJwt.js";

1 Like

thanks i was missing the import statement… silly me