BabylonJS/Babylon.js · error
Element "${element}" already added to the graph!
Error message
Element "${element}" already added to the graph! What it means
DependencyGraph.addElement stores elements in a Set and refuses to add the same element twice, throwing this error on duplicates. The graph assumes each element is registered exactly once before dependencies between elements are declared.
Source
Thrown at packages/dev/smartFilters/src/optimization/dependencyGraph.ts:26
private _dependOn: Map<T, Set<T>>;
private _requiredBy: Map<T, Set<T>>;
/**
* Creates a new instance of a dependency graph.
*/
constructor() {
this._list = new Set();
this._dependOn = new Map();
this._requiredBy = new Map();
}
/**
* Adds an element to the graph.
* @param element - The element to add to the graph.
*/
public addElement(element: T) {
if (this._list.has(element)) {
throw new Error(`Element "${element}" already added to the graph!`);
}
this._list.add(element);
}
/**
* Adds a dependency between two elements.
* @param element - The element that depends on another element.
* @param dependency - The element that is required by the element passed as the first parameter.
*/
public addDependency(element: T, dependency: T) {
if (!this._dependOn.has(element)) {
this._dependOn.set(element, new Set());
}
if (!this._requiredBy.has(dependency)) {
this._requiredBy.set(dependency, new Set());
}
View on GitHub (pinned to 0592b347b8)
Solutions
- Guard adds with a check (or wrap in a helper that skips already-added elements).
- Deduplicate your node list before building the graph.
- Make graph construction idempotent by tracking which elements you've already added.
Example fix
// before for (const node of allNodesIncludingShared) graph.addElement(node); // after for (const node of new Set(allNodesIncludingShared)) graph.addElement(node);
Defensive patterns
Strategy: validation
Validate before calling
if (!addedElements.has(node)) {
addedElements.add(node);
graph.addElement(node);
} Type guard
null
Try / catch
try {
graph.addElement(el);
} catch (e) {
if (!e.message.includes("already added to the graph")) throw e;
// duplicate is expected; skip
} Prevention
- Deduplicate node collections with new Set(nodes) before building the graph.
- Make graph construction functions idempotent.
- Remember Set membership is by reference for objects — reuse the same element instance.
When it happens
Trigger: Calling addElement twice with the same (equal-by-reference or equal-value) element — e.g. registering each graph node in a loop that re-visits a node, or calling addDependency setup twice on the same node set.
Common situations: Iterating a graph that contains shared/reused nodes; adding a block to the optimization dependency graph both directly and through a group; accidental double-invocation of a graph-construction function.
Related errors
- Initial color gradient values not found in context.
- SmartAssetManager: Unknown manager state.
- Circular dependency detected!
- _releaseTexture: Trying to release a texture from a non exis
- _releaseTexture: Can't find the texture in the pool ${textur
AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30).
Data as JSON: /api/errors/4c3ae1a81fff3103.
Report an issue: GitHub.