How to pass custom env variable to k6 script that's run via npm

Hi Team,

I have k6 scripts that as executed via npm. And when running the tests from azure via npm task the environment variables are not accessible.


let stages = [
    // ramp up users.
     {target: 10, duration: "10s"},
    // keep the users load for a certain duration.
    {target: 100, duration: 100s},
    // ramp down users.
    {target: 10, duration: "10s"}

let stages = __ENV.STAGES?  JSON.parse(__ENV.STAGES): stages

let optionsConfig = {
    setupTimeout: '600s',
    teardownTimeout: '1800s',
    thresholds: {
        'http_req_duration{name:CheckUserBalance}': ['p(95)<400'],
    stages: stages


 "scripts": {
    test:load:local: "k6 run ./scripts/test.js -e STAGES= "Stages_Array",
    test:load:azure: "k6 run ./scripts/test.js"

Local test execution

workingDirectory:  npm run  test:load:local --> The tests execute and the value of STAGES is used as passed in this script


  - name: TestScriptName
    type: string
    default:  test:load:azure
      - test:load:azure

 - name: Stages
    type: string
    default: NA

    - task: Npm@1
      displayName: 'Run K6 tests'
        command: custom
        workingDir: $(System.DefaultWorkingDirectory)
        verbose: true
        customCommand: run ${{ parameters.TestScriptName }}
        STAGES: ${{parameters.Stages}}

Azure execution
When the tests are executed from azure I get an error
level=error msg="TypeError: Cannot read property undefined

I know we could use this extension: k6 Load Testing - Visual Studio Marketplace and while using this i get another error ##[error]No agent found in pool load-test-vm which satisfies the specified demands: Go, Python, Agent.Version -gtVersion 2.182.1. Even when go, python and agent are installed correctly. What version of Go and Python are required for this?

Your help and support would be highly appreciated.


Hey @zaidmomin1

Sorry for the late reply on my part, it’s been a busy couple of weeks :bowing_man:

Unfortunately I’m not very familiar with Azure in general, and specifically with your setup. However, a question I would raise in the hope of maybe helping you figure it out:

  • are you sure the error message you see is due to the environment variable missing?
  • in your Azure.yaml, your - name: Stages i unindented, could it lead your env: statement in the last entry to actually not find it?
  • do you have a way to print the environment variable of the execution context Azure runs the k6 script from? I specifically mean that it would be worth verifying the environment variables set on the machine executing the script (not from within k6).

Hope that’s somehow helpful, let me know if you’ve made progress and if I can be of further help :bowing_man:

1 Like