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()` builtin (internal/js/bundle.go:482) reads files at script load time. It rejects an empty filename outright because there is nothing to resolve relative to the module's working directory. (A separate error covers calling open() outside the init context.)

Solutions

  1. Pass a real path relative to the script (or absolute, or a file:// URL).
  2. If the path comes from an env var, ensure it is set: `K6_DATA_FILE=users.csv k6 run script.js` and guard before calling open.
  3. For dynamically chosen files, resolve the name in setup() with http or compute it before calling open() in init context.

Example fix

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

Strategy: validation

Validate before calling

// guard before calling open() in init context
const path = __ENV.DATA_FILE;
if (!path || typeof path !== 'string') {
  throw new Error('DATA_FILE must be a non-empty environment variable');
}
const data = open(path);

Prevention

When it happens

Trigger: Calling `open('')`, or `open(someVar)` where someVar is an empty string — e.g. a path built from an env var or variable that is unset/empty at init time.

Common situations: Parameterized data files via `__ENV.DATA_FILE` when the variable isn't exported to the k6 process; template strings with an empty variable; refactor leftovers calling open with a placeholder ''.

Related errors


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

Appendix: source

Thrown at internal/js/bundle.go:484

			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 3fcf5388d7)