{"record":{"id":"ab9fb0b2530c4caa","repo":"grafana/k6","slug":"exported-setup-must-be-a-function","errorCode":null,"errorMessage":"exported 'setup' must be a function","messagePattern":"exported 'setup' must be a function","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/bundle.go","lineNumber":224,"sourceCode":"\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tdec := json.NewDecoder(bytes.NewReader(data))\n\t\t\t\tdec.DisallowUnknownFields()\n\t\t\t\tif err = dec.Decode(&b.Options); err != nil {\n\t\t\t\t\tif uerr := json.Unmarshal(data, &b.Options); uerr != nil {\n\t\t\t\t\t\t// Beautify the error so we can try to show the user the key and potential value that is failing to parse\n\t\t\t\t\t\tuerr = beautifyOptionsJSONUnmarshalError(data, uerr)\n\t\t\t\t\t\terr = errext.WithAbortReasonIfNone(\n\t\t\t\t\t\t\terrext.WithExitCodeIfNone(uerr, exitcodes.InvalidConfig),\n\t\t\t\t\t\t\terrext.AbortedByScriptError,\n\t\t\t\t\t\t)\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\t\t\t\t\tb.preInitState.Logger.WithError(err).Warn(\"There were unknown fields in the options exported in the script\")\n\t\t\t\t\terr = nil\n\t\t\t\t}\n\t\t\tcase consts.SetupFn:\n\t\t\t\terr = errors.New(\"exported 'setup' must be a function\")\n\t\t\t\treturn\n\t\t\tcase consts.TeardownFn:\n\t\t\t\terr = errors.New(\"exported 'teardown' must be a function\")\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n\t})\n\t<-ch\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif len(b.callableExports) == 0 {\n\t\treturn errors.New(\"no exported functions in script\")\n\t}\n\n\treturn nil\n}","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/bundle.go#L206-L242","documentation":"During bundle instantiation, populateExports (internal/js/bundle.go:224) walks the script's exports. Every export that is not a callable function is inspected; for the reserved name 'setup' a non-function value is rejected with this error, because k6 must call setup() once per test to build shared data.","triggerScenarios":"Exporting setup as a non-function: `export const setup = ...`, `export let setup = {}`, or `export { obj as setup }` where obj is not a function. Plain default/handle functions are unaffected.","commonSituations":"Refactoring scripts and turning setup into a data object; copy-paste from examples where setup was partially deleted; name collisions with an imported helper re-exported as setup.","solutions":["Declare setup as a function: `export function setup() { ... }`.","If setup is optional data, move it into a named export k6 ignores (e.g. `setupData`) and read it inside setup/default.","Check for accidental re-exports (`export * from './helpers'`) that publish a non-function `setup`."],"exampleFix":"// before\nexport const setup = { token: 'abc' };\n// after\nexport function setup() { return { token: 'abc' }; }","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// static self-check for k6 scripts: reserved exports must be functions\nconst fs = require('fs');\nconst src = fs.readFileSync('script.js', 'utf8');\nfor (const name of ['setup', 'teardown']) {\n  const bad = new RegExp(`export\\\\s+(const|let|var)\\\\s+${name}\\\\b`).test(src);\n  if (bad) throw new Error(`exported '${name}' must be a function`);\n}","tryCatchPattern":null,"preventionTips":["Use `export function setup()` always — never assign setup/teardown to variables.","Lint scripts in CI with a rule banning non-function exports named setup/teardown."],"tags":["script-structure","exports","setup","javascript"],"backgroundTag":"invalid-export-type","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}