{"record":{"id":"05d09657337a849d","repo":"grafana/k6","slug":"commonjs-s-exports-must-not-be-null","errorCode":null,"errorMessage":"CommonJS's exports must not be null","messagePattern":"CommonJS's exports must not be null","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"js/modules/cjsmodule.go","lineNumber":83,"sourceCode":"\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tmodule := rt.NewObject()\n\tcmi.exports = rt.NewObject()\n\t_ = module.Set(\"exports\", cmi.exports)\n\tcall, ok := sobek.AssertFunction(v)\n\tif !ok {\n\t\tpanic(\"Somehow a CommonJS module is not wrapped in a function - \" +\n\t\t\t\"this is a k6 bug, please report it (https://github.com/grafana/k6/issues)\")\n\t}\n\tif _, err = call(cmi.exports, module, cmi.exports); err != nil {\n\t\treturn nil, err\n\t}\n\n\texportsV := module.Get(\"exports\")\n\tif sobek.IsNull(exportsV) {\n\t\treturn nil, errors.New(\"CommonJS's exports must not be null\")\n\t}\n\tcmi.exports = exportsV.ToObject(rt)\n\n\t// this will be called multiple times we only need to update this on the first VU\n\tif cmi.w.exportedNames == nil {\n\t\tcmi.w.exportedNames = cmi.exports.Keys()\n\t\tif cmi.w.exportedNames == nil { // workaround if a CommonJS module does not have exports\n\t\t\tcmi.w.exportedNames = make([]string, 0)\n\t\t}\n\t\tfor _, callback := range cmi.w.exportedNamesCallbacks {\n\t\t\tcallback(cmi.w.exportedNames)\n\t\t}\n\t}\n\t__esModule := cmi.exports.Get(\"__esModule\") //nolint:revive\n\tcmi.isEsModuleMarked = __esModule != nil && __esModule.ToBoolean()\n\treturn cmi, nil\n}\n","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/js/modules/cjsmodule.go#L65-L101","documentation":"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.","triggerScenarios":"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.","commonSituations":"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'.","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"],"exampleFix":"// before\nmodule.exports = hasFeature ? featureApi : null;\n\n// after\nmodule.exports = hasFeature ? featureApi : {};","handlingStrategy":"validation","validationCode":"// scan required files before running k6\ngrep -rn 'module.exports *= *null' src/ && { echo 'null exports found'; exit 1; }","typeGuard":null,"tryCatchPattern":"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.","preventionTips":["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"],"tags":["javascript","commonjs","modules","require","import"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}