grafana/k6 · error

csv Parser constructor takes at least one non-nil source…

Error message

csv Parser constructor takes at least one non-nil source argument

What it means

Thrown (common.Throw, surfacing as a JS exception) by the experimental csv Parser constructor when it receives zero arguments or an explicit undefined first argument — an arity/nil guard. The constructor requires a source file as its first argument; the sibling guard above it already rejects init-context misuse, so reaching this error specifically means the source argument is missing/undefined at call time.

Solutions

  1. Pass an opened fs.File as the first argument: new Parser(await open('data.csv'))
  2. Check that the argument expression is not evaluating to undefined
  3. Do not attempt to construct the Parser with no arguments
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at internal/js/modules/k6/experimental/csv/module.go:159 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/d23f8cbfb448ff53. Report an issue: GitHub.

Appendix: source

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

		}
		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)),
		)
	}

	options := newDefaultParserOptions()
	if len(call.Arguments) == 2 && !sobek.IsUndefined(call.Argument(1)) {

View on GitHub (pinned to 01ffac6f24)