Can't use node fs module with k6

2.6k views Asked by At

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.

1

There are 1 answers

1
Михаил Стойков On BEST ANSWER

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.