Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use node fs module with k6

I have followed the instructions here and have setup webpack, but still couldn't use fs module to read the file.

Note: I am aware of open() function provided by k6 to read file but i want to check if the file exists before reading, because open() function throws go runtime error if file does not exist.

like image 256
user1415083 Avatar asked Oct 27 '25 21:10

user1415083


1 Answers

Using a native node module like fs isn't supported.

What you should probably use is a function wrapper around open to check if a file exists:

function exists(name) {
  try {
    open(name);
    return true;
  } catch(e) {
    return false;
  }
}

It is important also to note that open is only supported in the init context so you can't use it inside the default function. So if this is some integral part of your test you should probably rethink how it's done.

like image 60
Михаил Стойков Avatar answered Oct 30 '25 13:10

Михаил Стойков