BabylonJS/Babylon.js · error · Error

`Consts must have a name, type, and a default value: '${cons

Error message

`Consts must have a name, type, and a default value: '${constLine}'`

What it means

After locating an annotated const line, the converter parses it with /^const\s+(\w+)\s+(\w+)\s*=\s*([^\s;]+)\s*;?\s*$/. If the line does not match this exact 'const TYPE NAME = VALUE;' shape (or does not span a single line), this error names the offending line. The parser requires all three tokens plus a default value.

Source

Thrown at packages/dev/smartFilters/src/utils/buildTools/shaderConverter.ts:215

            continue;
        }

        const annotation = JSON.parse(annotationJSON.replace("//", "").trim()) as ConstMetadataAnnotation;

        // If the annotation doesn't have a "property" field, treat it as a regular const
        if (!annotation.property) {
            continue;
        }

        const constLine = matches[2];

        if (!constLine) {
            throw new Error("Const line not found");
        }

        const constLineMatches = new RegExp(/^const\s+(\w+)\s+(\w+)\s*=\s*([^\s;]+)\s*;?\s*$/gm).exec(constLine);
        if (!constLineMatches || constLineMatches.length < 4) {
            throw new Error(`Consts must have a name, type, and a default value: '${constLine}'`);
        }
        const type = constLineMatches[1];
        const friendlyName = constLineMatches[2];
        const defaultValue = constLineMatches[3];

        if (!friendlyName) {
            throw new Error(`Consts must have a name: '${constLine}'`);
        }
        if (defaultValue === null) {
            throw new Error(`Consts must have a value: '${constLine}'`);
        }

        const constProperty: ConstPropertyMetadata | null =
            type === "float"
                ? {
                      name: DecorateSymbol(friendlyName),
                      friendlyName,
                      type,

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Rewrite the const on a single line as `const <type> <name> = <value>;` with no internal whitespace in the value.
  2. Only use simple literal defaults (numbers) for property consts — the parser only supports float consts anyway.
  3. Remove trailing comments from the const line.
  4. If the value is a vector type, drop the property annotation or restructure as separate float consts.

Example fix

// before
const vec3 tint =
    vec3(1.0, 1.0, 1.0);
// after
const float tintR = 1.0;
const float tintG = 1.0;
const float tintB = 1.0;
Defensive patterns

Strategy: validation

Validate before calling

const bad = [...shader.matchAll(/\/\/\s*\{.*\}\s*(?:\r?\n)+(const .*)/gm)].map(m => m[1]).filter(l => !/^const\s+\w+\s+\w+\s*=\s*[^\s;]+\s*;?\s*$/.test(l));
if (bad.length) throw new Error(`Malformed property const lines: ${bad.join(" | ")}`);

Type guard

function isSingleLineConst(line: string): boolean {
  return /^const\s+\w+\s+\w+\s*=\s*[^\s;]+\s*;?\s*$/.test(line);
}

Try / catch

try {
  const info = ParseFragmentShader(blockName, namespace, shader);
} catch (e) {
  if (e instanceof Error && e.message.startsWith("Consts must have a name, type, and a default value")) {
    console.error("Fix this const line to match: const <type> <name> = <value>;", e.message);
  } else throw e;
}

Prevention

When it happens

Trigger: ParseFragmentShader encounters a property-annotated const written across multiple lines (`const float x = 0.5;`), with an unusual declaration form (`const float x=0.5` no spaces is fine, but `const vec3 x = vec3(0.);` — value with spaces/parens containing whitespace — breaks the [^\s;]+ value match), or missing the initializer entirely.

Common situations: Multi-line const initializers; vector/matrix consts written with spaces inside the initializer; consts like `const float PI = 3.14159 // radians;` with trailing comments; non-float consts such as vec2/vec3 which the format also can't express.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/d29ef001caf9384a. Report an issue: GitHub.