grafana/k6 · error
CommonJS's exports must not be null
Error message
CommonJS's exports must not be null
What it means
After executing a CommonJS module's factory function, k6 reads back the module's 'exports' property; if the script assigned module.exports = null, k6 refuses to load it because there is no object to import from. This check lives in the cjsmodule loader and fires during require()/import resolution, before the test runs.
Source
Thrown at js/modules/cjsmodule.go:83
if err != nil {
return nil, err
}
module := rt.NewObject()
cmi.exports = rt.NewObject()
_ = module.Set("exports", cmi.exports)
call, ok := sobek.AssertFunction(v)
if !ok {
panic("Somehow a CommonJS module is not wrapped in a function - " +
"this is a k6 bug, please report it (https://github.com/grafana/k6/issues)")
}
if _, err = call(cmi.exports, module, cmi.exports); err != nil {
return nil, err
}
exportsV := module.Get("exports")
if sobek.IsNull(exportsV) {
return nil, errors.New("CommonJS's exports must not be null")
}
cmi.exports = exportsV.ToObject(rt)
// this will be called multiple times we only need to update this on the first VU
if cmi.w.exportedNames == nil {
cmi.w.exportedNames = cmi.exports.Keys()
if cmi.w.exportedNames == nil { // workaround if a CommonJS module does not have exports
cmi.w.exportedNames = make([]string, 0)
}
for _, callback := range cmi.w.exportedNamesCallbacks {
callback(cmi.w.exportedNames)
}
}
__esModule := cmi.exports.Get("__esModule") //nolint:revive
cmi.isEsModuleMarked = __esModule != nil && __esModule.ToBoolean()
return cmi, nil
}
View on GitHub (pinned to 93accf6570)
Solutions
- Replace 'module.exports = null' with 'module.exports = {}' in the offending file
- If the assignment is conditional, export an empty object in the else branch instead of null
- For third-party code, vendor/patch it or import a different entry point that does not null its exports
Example fix
// before
module.exports = hasFeature ? featureApi : null;
// after
module.exports = hasFeature ? featureApi : {}; Defensive patterns
Strategy: validation
Validate before calling
// scan required files before running k6
grep -rn 'module.exports *= *null' src/ && { echo 'null exports found'; exit 1; } Try / catch
Wrap the require in a script-level check only if conditional: use a shim file that re-exports an object (module.exports = dep ?? {}) so the loaded module never presents null exports. Prevention
- Never assign null to module.exports; use {}
- Add an ESLint rule / grep in CI for 'module.exports = null'
- After adding bundlers, smoke-test that every bundled module still imports
When it happens
Trigger: A required/imported .js file contains 'module.exports = null' (directly, through a re-export chain, or via a bundler/transpiler that emits it). The error names the loader, and the failing file appears in the require chain.
Common situations: Conditional exports at the end of a library ('module.exports = featureEnabled ? lib : null'); Babel/tsc output for modules with no exports; hand-written stub files that use null as 'nothing to export'.
Related errors
- Error while parsing selector `${selector}` - selector cannot
- require() can't be used with an empty specifier
- stack too big
- the "require" function is only available in the init stage (
- "${attr}" attribute is only supported for roles: ${roles.sli
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/05d09657337a849d.
Report an issue: GitHub.