K6 dotenv support

Hi k6 community, I created a k6 extension that loads env vars from an .env file. I’m looking forward for your feedback…


When I try to use your library:

unknown module: k6/x/dotenv

1 Like

Same here. I got it working by building a custom k6 binary. Is that the only way to use this extension @szkiba?

Hello @PaulMEdwards. Welcome to the community! I can affirm that building the custom binary with xk6 is the only way to use the extension.

1 Like

Thank you for that confirmation, @javaducky.

What do you mean by “building the custom binary”

I would wish for this to be as simple as:

import * as dotenv from "k6/x/dotenv";

export default function (token) {
    console.log("the env var is ", dotenv.get("TEST_VAR"));

Here’s a painless solution to environment variables with k6; just use dotenv-cli.


Thanks for that suggestion @Murat! I’m sure many will find it useful.

For those who prefer to not install any external tools for this, loading an env file is also possible with some shell tricks.

Hi @szkiba
I have a problem using your module

import { parse } from "k6/x/dotenv";
dev_env = parse(".env.dev")

ERRO[0000] GoError: unknown module: k6/x/dotenv

So should I install & build it first?

Hi @nktedo001 !

You have to build a custom k6 binary to use this extension with the following command:

xk6 build --with github.com/szkiba/xk6-dotenv@latest
1 Like

how does this work on CI for example, will it still swap key values okay? Obviously there’s not going to be a .env file on CI…

Hello @bandorko , I am a new to k6. I have already built the extension in my system, how can I use this extension in my script now? Do I have to import this module? How can I access the variables stored in the .env file?

what’s the difference between using the latest and not? it’ll still take the latter one.
the problem here is that even after building a custom binary for k6 - it throws an error that the module is not found. any thoughts on fixing that?
@szkiba ?

It seems the latest version brings the removal of the JS API - which means you should no longer import anything from the js module but instead env variables are be loaded from .env* files by default and are available in _ENV.