# How to run node command in setup function?

**URL:** <https://community.grafana.com/t/how-to-run-node-command-in-setup-function/98366>\
**Category:** OSS Support\
**Created:** [March 25, 2022, 10:17pm UTC](https://community.grafana.com/t/how-to-run-node-command-in-setup-function/98366 "2022-03-25T22:17:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yusuf](https://avatars.discourse-cdn.com/v4/letter/y/c68b51/32.png) [@Yusuf](https://community.grafana.com/u/Yusuf)\
**Post date:** [March 25, 2022, 10:17pm UTC](https://community.grafana.com/t/how-to-run-node-command-in-setup-function/98366/1 "2022-03-25T22:17:02Z")

</div>

I want to run a function before k6 script. I thought that using node command in setup function (E.g. node test.js) This is possible? If it is not possible how can I run a function before k6 script ? And how I use that outside function ‘s returning value in setup? I m searching this because my function takes 5 minute to giving response for server. It is not meaningful using in k6 script so calling from response data outside is more effective

If there is any alternative idea could u give me a sample code or etc?

---

<div class="post-metadata">

**Author:** ![PaulM](https://avatars.discourse-cdn.com/v4/letter/p/839c29/32.png) [@PaulM](https://community.grafana.com/u/PaulM)\
**Post date:** [March 26, 2022, 6:06am UTC](https://community.grafana.com/t/how-to-run-node-command-in-setup-function/98366/2 "2022-03-26T06:06:10Z")

</div>

Hi @Yusuf !  
Look at [this](https://github.com/grafana/k6-example-woocommerce/blob/5e402bfab36d3cefa0d668d4de198b6c3b20aebd/main.js#L5) example, maybe it can help you.  
Example:

```auto
import { addToCart } from "./addToCart.js"; // import js file with function

export default function main() {
  
  addToCart(); //function call

}

```

---

<div class="post-metadata">

**Author:** ![Yusuf](https://avatars.discourse-cdn.com/v4/letter/y/c68b51/32.png) [@Yusuf](https://community.grafana.com/u/Yusuf)\
**Post date:** [March 26, 2022, 10:22am UTC](https://community.grafana.com/t/how-to-run-node-command-in-setup-function/98366/3 "2022-03-26T10:22:04Z")

</div>

Hi @PaulM thanks for reply , I know this method but the main case is like below;

I should run a function outside of k6 script . And I have to call returning value in k6. Because I must run that function one time . If I call in export default function it will work more than one time . It should work one time . When running in setup it passes the default 60 sn so giving timeout error. Because of this I should find a solution like this;  
1.Running function outside  
2. Pushing the json value to an array (I will set the returning value in outside function)  
3. I will call the array in setup function

When I use this methods , function run one time outside of k6 script. I will only take returning array in k6 script. I m searching a code like this 🙂

---

<div class="post-metadata">

**Author:** ![PaulM](https://avatars.discourse-cdn.com/v4/letter/p/839c29/32.png) [@PaulM](https://community.grafana.com/u/PaulM)\
**Post date:** [March 26, 2022, 12:13pm UTC](https://community.grafana.com/t/how-to-run-node-command-in-setup-function/98366/4 "2022-03-26T12:13:19Z")

</div>

@Yusuf One of the options is to output data to a file with one script. Another script unloads the required values ​​from the file.  
Example:

1. To write to the file, you will need to display the required values ​​in log `cosole.log(response)` and run the script with parameters `k6 run test.js 2>resp.txt` and then use the resp.txt file. And example [this](https://community.grafana.com/t/print-http-debug-logs-into-file-print-all-api-responses-in-a-file/99259/) and [this](https://community.grafana.com/t/write-response-body-to-file/99594/)
2. Try to use the setup function. @ned [wrote about the possibility of overriding the timeout.](https://community.grafana.com/t/how-to-solve-request-timeout-error-in-setup/99350/8).

```auto
import http from 'k6/http';

export function setup() {
  let params = {
    timeout: "120s"
  };

  const res = http.get('https://httpbin.org/get', params);
  return { data: res.json() };
}

export default function (data) {
  console.log(JSON.stringify(data));
}

```

1. Change the timeout in the default function as described above and place the data acquisition in 1 iteration.

```auto
if(__ITER == 0){

// Get values ​​here
}

```

---

<div class="post-metadata">

**Author:** ![Yusuf](https://avatars.discourse-cdn.com/v4/letter/y/c68b51/32.png) [@Yusuf](https://community.grafana.com/u/Yusuf)\
**Post date:** [March 26, 2022, 7:08pm UTC](https://community.grafana.com/t/how-to-run-node-command-in-setup-function/98366/5 "2022-03-26T19:08:05Z")

</div>

Thanks for help @PaulM

---

<div class="post-metadata">

**Author:** ![Yusuf](https://avatars.discourse-cdn.com/v4/letter/y/c68b51/32.png) [@Yusuf](https://community.grafana.com/u/Yusuf)\
**Post date:** [March 26, 2022, 8:19pm UTC](https://community.grafana.com/t/how-to-run-node-command-in-setup-function/98366/6 "2022-03-26T20:19:44Z")

</div>

@PaulM When I use the timeout like below the setup time does not takes 4m=240000ms.Takes again 60s.Tried also timeout:“240000ms”;

```auto
export function setup() {
  let createPickingStartedSalesOrdersRes = http.post(
    Load.api + endpoints.SalesOrderLoadSeed.CreatePickingCompletedSalesOrders,
    JSON.stringify({
      salesOrderConfigurations: [
        {
          amount: 500,
        },
      ],
      tenant: "test",
     
    }),
    {
      headers: {
        apikey: "*",
        "Content-Type": "application/json",
      },
      timeout:240000
    }
  );
  let operatorAuth = Load.auth(Roles.Operator);
  let supportAuth = Load.auth(Roles.Support);
  return [
    operatorAuth.json(),
    supportAuth.json(),
    createPickingStartedSalesOrdersRes.json(),
    
  ];
}

```

---

<div class="post-metadata">

**Author:** ![PaulM](https://avatars.discourse-cdn.com/v4/letter/p/839c29/32.png) [@PaulM](https://community.grafana.com/u/PaulM)\
**Post date:** [March 26, 2022, 10:07pm UTC](https://community.grafana.com/t/how-to-run-node-command-in-setup-function/98366/7 "2022-03-26T22:07:45Z")

</div>

@Yusuf Is it possible that a timeout of 60s is set on the server under test?  
Please check this code:

```auto
export function setup() {

let url = Load.api + endpoints.SalesOrderLoadSeed.CreatePickingCompletedSalesOrders;

let data = JSON.stringify({
  salesOrderConfigurations: [
    {
      amount: 500,
    },
  ],
  tenant: "test",
 
});

let params = {
  headers: { apikey: "*", "Content-Type": "application/json"},
    timeout: "120s",

};

  let createPickingStartedSalesOrdersRes = http.post(url, data, params);
  let operatorAuth = Load.auth(Roles.Operator);
  let supportAuth = Load.auth(Roles.Support);
  return [
    operatorAuth.json(),
    supportAuth.json(),
    createPickingStartedSalesOrdersRes.json(),
    
  ];
}

```

@ned @inanc Can you please tell me if we are redefining the timeout for the request correctly?

---

<div class="post-metadata">

**Author:** ![nedyalko](https://avatars.discourse-cdn.com/v4/letter/n/d26b3c/32.png) [@nedyalko](https://community.grafana.com/u/nedyalko)\
**Post date:** [March 29, 2022, 6:34am UTC](https://community.grafana.com/t/how-to-run-node-command-in-setup-function/98366/8 "2022-03-29T06:34:42Z")

</div>

The request timeout looks correct, but consider that `setup()` itself might also time out. That behavior is determined by the `setupTimeout` global option, see [Options reference](https://k6.io/docs/using-k6/options/#setup-timeout)
