grafana/k6 · error

you are trying to access identifier %q, this likely is due t

Error message

you are trying to access identifier %q, this likely is due to mixing ECMAScript Modules (ESM) and CommonJS syntax. This isn't supported in the JavaScript standard, please use only one or the other

What it means

To catch module-system mixing, setInitGlobals installs accessor traps (warnAboutModuleMixing) on the `module` and `exports` globals. Reading or writing either identifier in a script invokes a function that returns this error, because a file cannot legitimately be both an ES module (import/export) and CommonJS (require/module.exports) - the ECMAScript standard forbids it and k6's ESM transform would otherwise misbehave silently.

Source

Thrown at internal/js/bundle.go:509

			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()")
				}
			}
		}

		return openImpl(rt, b.filesystems["file"], pwd, filename, args...)
	})
	warnAboutModuleMixing := func(name string) {
		warnFunc := rt.ToValue(func() error {
			return fmt.Errorf(
				"you are trying to access identifier %q, this likely is due to mixing "+
					"ECMAScript Modules (ESM) and CommonJS syntax. "+
					"This isn't supported in the JavaScript standard, please use only one or the other",
				name)
		})
		err := rt.GlobalObject().DefineAccessorProperty(name, warnFunc, warnFunc, sobek.FLAG_FALSE, sobek.FLAG_FALSE)
		if err != nil {
			panic(fmt.Errorf("failed to set '%s' global object: %w", name, err))
		}
	}
	warnAboutModuleMixing("module")
	warnAboutModuleMixing("exports")

	rt.SetFinalImportMeta(func(o *sobek.Object, mr sobek.ModuleRecord) {
		err := o.Set("resolve", func(specifier string) (string, error) {
			u, err := modSys.Resolve(mr, specifier)
			if err != nil {
				return "", err

View on GitHub (pinned to 93accf6570)

Solutions

  1. Pick one module system per file: in a file that uses import/export, remove every `module`/`exports` reference
  2. Replace `module.exports = { x }` with `export const x` or `export default`
  3. Replace require() with import in the same file, since the mixed variants break resolution too

Example fix

// before: ESM import plus CommonJS export -> trap fires
import http from 'k6/http';
module.exports = { doThing: () => http.get('https://k6.io') };

// after: single module system (ESM)
import http from 'k6/http';
export const doThing = () => http.get('https://k6.io');
Defensive patterns

Strategy: validation

Validate before calling

# flag files that mix module systems: ESM syntax AND module/exports references
for f in *.js; do
  grep -qE '^[[:space:]]*(import|export)[[:space:]]' "$f" || continue
  grep -qE '\b(module|exports)\b\s*[.=]' "$f" && echo "mixed module systems: $f"
done

Prevention

When it happens

Trigger: A script that uses import/export and also references `module` or `exports`: `module.exports = {...}` copied from CommonJS, `exports.default = ...` left over from a Babel/Jest template, or destructuring from `module`.

Common situations: Copy-pasting Node/Jest/Mocha snippets into k6 scripts; converting an old CommonJS k6 script to ESM halfway through; bundler output that references `exports`.

Related errors


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