Support for dynamic import()?


I’m currently attempting to include some browserified node modules (crypto and others) into my k6 script. While i’ve gotten it to “work” I have found there is a performance penalty per VU. I have tried to limit the use of my import and came upon one idea was to conditionally import inside of the setup function (presumably this may run the import only once instead of in each VU’s JS environment which doesn’t need the import).

Something like this (contrived example):

// global setup, called once
export function setup () {
  let done = false;
  if (createData) {
      const crypto = import('./utils/crypto.dist.js').then((mod) => {
         done = true;
         return mod;

      while (!done) {
        setTimeout(() => { console.log("loading..."); }, 1000);
      // do something with crypto

However it appears the dynamic import() function is not supported in k6? I get unexpected token on the ^import line from babel. Any ideas would be appreciated how I could either dynamically import (if it’s possible) or perhaps another way to reduce the per VU overhead of importing large modules. xk6/crypto doesn’t support my use case unfortunately. Thanks!

k6 doesn’t support dynamic import() yet, sorry. However, k6 has a require() function that’s somewhat different from the Node.js one (and a bit of a hack :sweat_smile:). We don’t have long-term compatibility guarantees about it, but it might be of use to you until we have import().

Here are the scant docs about it, and despite the fact that it’s documented in the --compatibility-mode=base section, it’s available in --compatibility-mode=extended (the default) as well. Again, to be clear, we reserve the right to deprecate it in the future, but we’ll give plenty of warnings about it if we do so.

1 Like