grafana/k6 · error

csv Parser constructor must be called in the init context

Error message

csv Parser constructor must be called in the init context

What it means

The experimental CSV Parser reads and parses the entire file once during script initialization for performance. Its constructor therefore rejects instantiation when vu.State() is non-nil, i.e. when the constructor runs inside the VU (default function) context instead of the init context.

Source

Thrown at internal/js/modules/k6/experimental/csv/module.go:155

			callback(func() error {
				return reject(rt.NewGoError(err))
			})
			return
		}
		callback(func() error {
			return resolve(fn())
		})
	}()

	return promise, nil
}

// NewParser creates a new CSV parser instance.
func (mi *ModuleInstance) NewParser(call sobek.ConstructorCall) *sobek.Object {
	rt := mi.vu.Runtime()

	if mi.vu.State() != nil {
		common.Throw(rt, errors.New("csv Parser constructor must be called in the init context"))
	}

	if len(call.Arguments) < 1 || sobek.IsUndefined(call.Argument(0)) {
		common.Throw(rt, fmt.Errorf("csv Parser constructor takes at least one non-nil source argument"))
	}

	fileArg := call.Argument(0)
	if common.IsNullish(fileArg) {
		common.Throw(rt, fmt.Errorf("csv Parser constructor takes at least one non-nil source argument"))
	}

	// 1. Make sure the Sobek object is a fs.File (Sobek operation)
	var file fs.File
	if err := mi.vu.Runtime().ExportTo(fileArg, &file); err != nil {
		common.Throw(
			mi.vu.Runtime(),
			fmt.Errorf("first argument expected to be a fs.File instance, got %T instead", call.Argument(0)),
		)

View on GitHub (pinned to 93accf6570)

Solutions

  1. Instantiate the Parser at the top level of the script (init context)
  2. Open the fs file in the init context too, since fs.open() is init-only as well
  3. If per-iteration reset is needed, use parser.seek(0, SeekMode.Start) or create one Parser and share it

Example fix

// before
export default function () {
  const parser = new csv.Parser(file);
  // ...
}

// after
const parser = new csv.Parser(file);
export default function () {
  // use parser here
}
Defensive patterns

Strategy: validation

Validate before calling

// Structural check: construction must sit at module top level.
// Cheap runtime guard for factory wrappers:
function makeParser(file) {
  if (typeof (globalThis).Symbol === 'undefined') { /* init context heuristics */ }
  return new csv.Parser(file);
}

Prevention

When it happens

Trigger: Calling new csv.Parser(file) inside export default function() or any function invoked per iteration; constructing the Parser lazily inside a helper that runs during the test.

Common situations: Refactoring a script and moving setup code into the default function; creating a Parser per iteration to 'reset' it instead of seeking; wrapping construction in a factory called from the default function.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/00312a5789d9d634. Report an issue: GitHub.