grafana/k6 · critical · InvalidSelectorError
Error while parsing selector `${selector}` - selector cannot
Error message
Error while parsing selector `${selector}` - selector cannot be empty What it means
An internal-invariant Go panic in cjsmodule.Evaluate (js/modules/cjsmodule.go:74). k6 compiles CommonJS sources only after the loader has wrapped them in a function body (the classic `function(exports, require, module, __filename, __dirname){...}` form), so rt.RunProgram must return a callable; sobek.AssertFunction failing means the wrapping/compilation pipeline produced something that is not a function. The message itself states this is a k6 bug, not a script-author error, and asks for a report — the panic is not a catchable JS exception.
Source
Thrown at internal/js/modules/k6/browser/common/js/injected_script.js:1141
eat1();
if (operator !== "=" && typeof value !== "string")
throw new InvalidSelectorError(`Error while parsing selector \`${selector}\` - cannot use ${operator} in attribute with non-string matching value - ${value}`);
return { name: jsonPath.join("."), jsonPath, op: operator, value, caseSensitive };
}
const result = {
name: "",
attributes: []
};
result.name = readIdentifier();
skipSpaces();
while (next() === "[") {
result.attributes.push(readAttribute());
skipSpaces();
}
if (!EOL)
syntaxError(void 0);
if (!result.name && !result.attributes.length)
throw new InvalidSelectorError(`Error while parsing selector \`${selector}\` - selector cannot be empty`);
return result;
}
// packages/injected/src/roleSelectorEngine.ts
var kSupportedAttributes = ["selected", "checked", "pressed", "expanded", "level", "disabled", "name", "include-hidden"];
kSupportedAttributes.sort();
function validateSupportedRole(attr, roles, role) {
if (!roles.includes(role))
throw new Error(`"${attr}" attribute is only supported for roles: ${roles.slice().sort().map((role2) => `"${role2}"`).join(", ")}`);
}
function validateSupportedValues(attr, values) {
if (attr.op !== "<truthy>" && !values.includes(attr.value))
throw new Error(`"${attr.name}" must be one of ${values.map((v) => JSON.stringify(v)).join(", ")}`);
}
function validateSupportedOp(attr, ops) {
if (!ops.includes(attr.op))
throw new Error(`"${attr.name}" does not support "${attr.op}" matcher`);
}View on GitHub (pinned to 93accf6570)
Solutions
- Report it: open an issue at https://github.com/grafana/k6/issues with the script, the require'd module, and `k6 run --verbose` output
- Reproduce with a minimal CommonJS file (module.exports = {}) to confirm it is not your script
- Try a different k6 version — check the release notes for CJS/sobek loader fixes and upgrade or pin accordingly
- Convert the affected module to an ES module (export/import) to bypass the cjsModule path entirely
- If running a fork, verify any changes to the source-wrapping code in the loader
Example fix
// before (script.cjs-style dependency loaded through the CJS pipeline)
const lib = require('./legacy-lib.js'); // panics inside cjsmodule.Evaluate
// after (bypass the CommonJS wrapper by using ESM)
import * as lib from './legacy-lib.mjs'; // requires converting the module to export/import Defensive patterns
Strategy: fallback
Validate before calling
null
Try / catch
// This is a Go panic inside k6's module evaluation, not a JS exception;
// try/catch in the script cannot intercept it. The only guards are
// pre-run: pin a known-good k6 version and verify the CJS dependency loads:
// k6 run --verbose smoke-cjs.js (smoke-cjs.js: const lib = require('./lib.js'); export default function() {}) Prevention
- Prefer ES modules (import/export) over require() so the cjsModule wrapper path is never used
- Pin the k6 version in CI after verifying CommonJS dependencies load
- When upgrading k6, smoke-test scripts that require() local or npm CommonJS modules before rolling out
- Report the panic with a minimal reproducer so the loader regression gets fixed upstream
When it happens
Trigger: Requiring or importing a CommonJS module from a local file or npm-style dependency, where the wrapped program evaluates to a non-function due to a k6 loader defect (e.g. a change in how sources are wrapped, a sobek/goja upgrade, or an unusual module cache state). Nothing in a well-formed script can intentionally cause it.
Common situations: Hitting a regression in a specific k6 build while loading CommonJS npm dependencies via require(); running a forked or locally modified k6 where the CJS wrapper was altered; bisecting crashes after upgrading k6 or its JS runtime dependency. Essentially always accompanied by 'please report it' — users see it right at startup or at first require().
Related errors
- "${attr}" attribute is only supported for roles: ${roles.sli
- CommonJS's exports must not be null
- the "require" function is only available in the init stage (
- Error while parsing selector `${selector}` - cannot use ${op
- require() can't be used with an empty specifier
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/6d449530cfd221a8.
Report an issue: GitHub.