grafana/k6 · error

no exported functions in script

Error message

no exported functions in script

What it means

After validating exports, populateExports (internal/js/bundle.go:238) requires at least one callable export (default, handle, setup, teardown, or scenario exec functions). A script that exports only data/objects has nothing for k6 to execute per-iteration, so the bundle is rejected with 'no exported functions in script'.

Solutions

  1. Add an entry point: `export default function () { ... }`.
  2. Or define scenario exec functions and reference them in options.scenarios.
  3. Verify the module format matches the file (ESM `export` vs CJS `module.exports`) and that the file has the right extension for the compatibility mode.

Example fix

// before
export const options = { vus: 1, duration: '10s' };
// after
export const options = { vus: 1, duration: '10s' };
export default function () { http.get('https://test.k6.io'); }
Defensive patterns

Strategy: validation

Validate before calling

# quick pre-run sanity check in CI
if ! grep -qE 'export\s+(default\s+)?(async\s+)?function|export\s+\{[^}]*\b(handle|setup|teardown)\b' script.js; then
  echo 'script has no exported functions for k6 to execute'; exit 1
fi

Prevention

When it happens

Trigger: A script whose only export is `export const options = {...}`; scripts that define functions but forget `export`; using `module.exports` in an ES module (or vice versa) so exports don't register; importing everything but exporting nothing.

Common situations: Options-only config files passed to `k6 run`; refactoring where `export default function () {...}` was moved into a helper file; module-format mismatches (ESM vs CommonJS under different compatibility modes).

Related errors


AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18). Data as JSON: /api/errors/68ca62cb445740cd. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/bundle.go:238

					b.preInitState.Logger.WithError(err).Warn("There were unknown fields in the options exported in the script")
					err = nil
				}
			case consts.SetupFn:
				err = errors.New("exported 'setup' must be a function")
				return
			case consts.TeardownFn:
				err = errors.New("exported 'teardown' must be a function")
				return
			}
		}
	})
	<-ch
	if err != nil {
		return err
	}

	if len(b.callableExports) == 0 {
		return errors.New("no exported functions in script")
	}

	return nil
}

func beautifyOptionsJSONUnmarshalError(data []byte, err error) error {
	unmarshalTypError := new(json.UnmarshalTypeError)
	if errors.As(err, &unmarshalTypError) {
		e := unmarshalTypError
		previousNewLineIndex := max(bytes.LastIndexByte(data[:e.Offset], '\n'), 0)
		nextNewLineIndex := max(min(bytes.IndexByte(data[e.Offset:], '\n'), len(data)-1), int(e.Offset))

		info := strings.TrimSpace(string(data[previousNewLineIndex:nextNewLineIndex]))
		err = fmt.Errorf("parsing options from script got error while parsing %q: %w", info, e)
	}
	return err
}

View on GitHub (pinned to 3fcf5388d7)