grafana/k6 · error

exported 'teardown' must be a function

Error message

exported 'teardown' must be a function

What it means

populateExports (internal/js/bundle.go:227) enforces that the reserved export name 'teardown' is a function, since k6 invokes teardown(data) exactly once at test end to clean up. Any non-callable value exported as teardown aborts script loading with this error.

Solutions

  1. Declare it as a function: `export function teardown(data) { ... }`.
  2. Remove the export entirely if there is no cleanup to do.
  3. Fix accidental re-exports of a non-function teardown symbol.

Example fix

// before
export const teardown = () => {};  // exported arrow is fine, but `= {}` or `= null` fails
// after
export function teardown(data) { /* cleanup */ }
Defensive patterns

Strategy: type-guard

Type guard

// reject non-function teardown exports before shipping
const src = require('fs').readFileSync('script.js', 'utf8');
if (/export\s+(const|let|var)\s+teardown\b/.test(src)) {
  throw new Error("exported 'teardown' must be a function");
}

Prevention

When it happens

Trigger: `export const teardown = null`, `export let teardown = {}`, or re-exporting a non-function symbol named teardown from another module (`export { cleanupMap as teardown }`).

Common situations: Commenting out the teardown body but leaving a placeholder variable; merging modules where teardown collides with a helper object; partial migration from other harnesses.

Related errors


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

Appendix: source

Thrown at internal/js/bundle.go:227

				dec.DisallowUnknownFields()
				if err = dec.Decode(&b.Options); err != nil {
					if uerr := json.Unmarshal(data, &b.Options); uerr != nil {
						// Beautify the error so we can try to show the user the key and potential value that is failing to parse
						uerr = beautifyOptionsJSONUnmarshalError(data, uerr)
						err = errext.WithAbortReasonIfNone(
							errext.WithExitCodeIfNone(uerr, exitcodes.InvalidConfig),
							errext.AbortedByScriptError,
						)
						return
					}
					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)

View on GitHub (pinned to 3fcf5388d7)