Browserify and import from k6

I’m using browserify as suggested in the modules docs but I’m hitting this error:

browserify -s foobar normal-user.js -o k6-browserified-source.js
Error: Cannot find module ‘k6’ from ‘/…/k6/my-tests’

Any tips on how to browserify code that’s using the k6 builtin module?

I would love a working example code I can download with the commands to run in k6’s cloud (loadimpact) and that uses modular code - are there any available?

Also: Am I correct in my understanding that:

  1. k6 cloud only accepts one file.js
  2. we are trying to use browserify to generate a single js file for the whole application? i.e. if I import/require files from my filesystem we instead use browserify to bundle all those files together in to one file?
1 Like

That is incorrect :slightly_smiling_face: You specify one file in the k6 cloud command, but that file can import other files without any issues. So, if you have a main.js file that imports a lib.js, when you execute k6 cloud main.js, k6 would bundle both files (and some metadata) and send them to the Load Impact cloud to be executed.

You can inspect what the bundle contains by running k6 archive main.js locally and examining the resulting .tar file. More information about it here:

Regarding browserify, I’m not sure how it works, so someone else would need to answer. But you can see a Webpack example that @mstoykov made here: GitHub - MStoykov/k6-es6: experiement with k6 and npm to make any(most) es6 code work under k6


Well that’s a great answer @ned, thank you very much.

Well in that case I’m confused what/why we need to use browserify. My initial guess is the files archived by k6 cloud only go one level deep so we need to bundle any modules into a single file that we can then import as single files into out entrypoint.js

browserify might be useful when you want to reuse some JS libraries written for node.js, because it deals with things like the node module resolution algorithm for require() and with shimming some of the node system modules with pure JS alternatives.


And to re-iterate - k6 cloud should be able to deal with all imports in your script, regardless of how many levels deep they are. If it doesn’t, then I’d treat it as a bug.

1 Like

@ryan: Just to chime in on this topic, I’d recommend you to use the k6-es6 as a template (or an equivalent webpack + babel setup, if you prefer to set it up yourself) anyway, as it’s proven to be a lot more resource efficient and flexible than the browserify dito.