grafana/k6 · error

open() can't be used with an empty filename

Error message

open() can't be used with an empty filename

What it means

The init-context open() helper refuses an empty filename before touching the filesystem. open() loads files (json/csv/txt/js/bin, read-only) during initialization so their contents can be used in VU iterations.

Source

Thrown at internal/js/bundle.go:482

			panic(fmt.Errorf("failed to set '%s' global object: %w", k, err))
		}
	}

	impl := requireImpl{
		inInitContext: func() bool { return vu.state == nil },
		modSys:        modSys,
	}

	mustSet("require", impl.require)

	mustSet("open", func(filename string, args ...string) (sobek.Value, error) {
		// TODO fix in stack traces
		if vu.state != nil {
			return nil, fmt.Errorf(cantBeUsedOutsideInitContextMsg, "open")
		}

		if filename == "" {
			return nil, errors.New("open() can't be used with an empty filename")
		}
		// This uses the pwd from the requireImpl
		pwd, err := modSys.CurrentlyRequiredModule()
		if err != nil {
			return nil, err
		}
		if !strings.HasPrefix(filename, "file://") && !filepath.IsAbs(filename) {
			otherPath, shouldWarn := modSys.ShouldWarnOnParentDirNotMatchingCurrentModuleParentDir(vu, pwd)
			logger := b.preInitState.Logger
			if shouldWarn {
				logger.Warningf("open() was used and is currently relative to '%s', but in the future "+
					"it will be aligned with how `require` and imports work and will be relative to '%s'. This means "+
					"that in the future open will open relative path relative to the module/file it is written in. "+
					"You can future proof this by using `import.meta.resolve()` to get relative paths to the file it "+
					"is written in the current k6 version.", pwd, otherPath)
				err = b.preInitState.Usage.Uint64("deprecations/openRelativity", 1)
				if err != nil {
					logger.WithError(err).Warn("failed reporting usage of deprecated relativity of open()")

View on GitHub (pinned to 93accf6570)

Solutions

  1. Validate the variable first: `if (!__ENV.DATA_FILE) throw new Error('DATA_FILE is required');`
  2. Export the env var when launching: `DATA_FILE=./users.csv k6 run script.js`
  3. Pass a literal relative or absolute path with a supported extension

Example fix

// before
const data = open(__ENV.DATA_FILE);

// after
if (!__ENV.DATA_FILE) throw new Error('DATA_FILE is required');
const data = open(__ENV.DATA_FILE);
Defensive patterns

Strategy: validation

Validate before calling

// Only call open() with a concrete, non-empty path
const file = __ENV.DATA_FILE;
if (!file || typeof file !== 'string') throw new Error('DATA_FILE env var must be set to a non-empty path');
const data = open(file);

Type guard

const isValidOpenPath = (p) => typeof p === 'string' && p.length > 0 && /\.(js|json|txt|bin|csv)$/.test(p);

Prevention

When it happens

Trigger: `open('')` or `open(pathVar)` where pathVar is the empty string — typically a path built from an unset `__ENV` variable or a destructuring/template-string mistake; fires only in the init context (outside it you get the 'outside init context' error instead).

Common situations: `open(__ENV.DATA_FILE)` with DATA_FILE not exported to the k6 process; template literals with a missing variable interpolating to ''.

Related errors


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