{"record":{"id":"7e89c595467940da","repo":"grafana/k6","slug":"require-can-t-be-used-with-an-empty-specifier","errorCode":null,"errorMessage":"require() can't be used with an empty specifier","messagePattern":"require\\(\\) can't be used with an empty specifier","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"js/modules/require_impl.go","lineNumber":24,"sourceCode":"\t\"maps\"\n\t\"net/url\"\n\t\"strings\"\n\n\t\"github.com/grafana/sobek\"\n\n\t\"go.k6.io/k6/v2/internal/loader\"\n)\n\n// Require is the actual call that implements require\nfunc (ms *ModuleSystem) Require(specifier string) (*sobek.Object, error) {\n\tif !ms.resolver.locked {\n\t\tif err := ms.resolver.usage.Uint64(\"usage/require\", 1); err != nil {\n\t\t\tms.resolver.logger.WithError(err).Warn(\"couldn't report usage\")\n\t\t}\n\t}\n\n\tif specifier == \"\" {\n\t\treturn nil, errors.New(\"require() can't be used with an empty specifier\")\n\t}\n\n\trt := ms.vu.Runtime()\n\tparentModuleStr := getCurrentModuleScript(ms.vu)\n\n\tparentModule, _ := ms.resolver.sobekModuleResolver(nil, parentModuleStr)\n\tm, err := ms.resolver.sobekModuleResolver(parentModule, specifier)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tif wm, ok := m.(*goModule); ok {\n\t\tvar gmi *goModuleInstance\n\t\tgmi, err = ms.getModuleInstanceFromGoModule(wm)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\texports := toESModuleExports(gmi.mi.Exports())\n\t\treturn rt.ToValue(exports).ToObject(rt), nil","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/js/modules/require_impl.go#L6-L42","documentation":"ModuleSystem.Require() implements require() and refuses an empty specifier string before any resolution is attempted, because there is no module that an empty string could name. The check also runs after the usage-reporting step, so it fires on every require('') call regardless of the resolver state.","triggerScenarios":"require('') literally, or require(variable) where variable is '' — commonly a module name built from env vars, config maps, or string concatenation that produced an empty string.","commonSituations":"Feature-flag-driven requires (require(FLAGS.feature ? './a' : '')); iterating a config list where one entry is missing; typos like require(`${prefix}`) with an empty prefix.","solutions":["Pass a real module specifier: require('./helpers.js')","Guard dynamic specifiers: if (!specifier) throw new Error(`empty module specifier for feature ${feature}`)","Fix the data source (env var / config key) that produced the empty string"],"exampleFix":"// before\nconst mod = require(process.env.K6_MOD || '');\n\n// after\nconst spec = process.env.K6_MOD;\nif (!spec) throw new Error('K6_MOD must name a module');\nconst mod = require(spec);","handlingStrategy":"validation","validationCode":"function safeRequire(spec) {\n  if (typeof spec !== 'string' || spec === '') {\n    throw new Error(`invalid require specifier: ${JSON.stringify(spec)}`);\n  }\n  return require(spec);\n}","typeGuard":"const isSpecifier = (s) => typeof s === 'string' && s.trim().length > 0;","tryCatchPattern":null,"preventionTips":["Validate module names from env vars/config before require()","Avoid ternaries that can yield '' as a specifier","Fail fast with context instead of letting require('') surface"],"tags":["javascript","modules","require","validation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}