grafana/k6 · error
require() can't be used with an empty specifier
Error message
require() can't be used with an empty specifier
What it means
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.
Source
Thrown at js/modules/require_impl.go:24
"maps"
"net/url"
"strings"
"github.com/grafana/sobek"
"go.k6.io/k6/v2/internal/loader"
)
// Require is the actual call that implements require
func (ms *ModuleSystem) Require(specifier string) (*sobek.Object, error) {
if !ms.resolver.locked {
if err := ms.resolver.usage.Uint64("usage/require", 1); err != nil {
ms.resolver.logger.WithError(err).Warn("couldn't report usage")
}
}
if specifier == "" {
return nil, errors.New("require() can't be used with an empty specifier")
}
rt := ms.vu.Runtime()
parentModuleStr := getCurrentModuleScript(ms.vu)
parentModule, _ := ms.resolver.sobekModuleResolver(nil, parentModuleStr)
m, err := ms.resolver.sobekModuleResolver(parentModule, specifier)
if err != nil {
return nil, err
}
if wm, ok := m.(*goModule); ok {
var gmi *goModuleInstance
gmi, err = ms.getModuleInstanceFromGoModule(wm)
if err != nil {
return nil, err
}
exports := toESModuleExports(gmi.mi.Exports())
return rt.ToValue(exports).ToObject(rt), nilView on GitHub (pinned to 93accf6570)
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
Example fix
// before
const mod = require(process.env.K6_MOD || '');
// after
const spec = process.env.K6_MOD;
if (!spec) throw new Error('K6_MOD must name a module');
const mod = require(spec); Defensive patterns
Strategy: validation
Validate before calling
function safeRequire(spec) {
if (typeof spec !== 'string' || spec === '') {
throw new Error(`invalid require specifier: ${JSON.stringify(spec)}`);
}
return require(spec);
} Type guard
const isSpecifier = (s) => typeof s === 'string' && s.trim().length > 0;
Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- CommonJS's exports must not be null
- stack too big
- Error while parsing selector `${selector}` - selector cannot
- exported 'setup' must be a function
- exported 'teardown' must be a function
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/7e89c595467940da.
Report an issue: GitHub.