BabylonJS/Babylon.js · error

Service '${contract.toString()}' has not been instantiated i

Error message

Service '${contract.toString()}' has not been instantiated in the '${this._friendlyName}' container.

What it means

_resolveDependency found a registration for the contract in _serviceDefinitions but no instantiated instance in _serviceInstances for the consumer. In this container the contract is registered but its producing service was never instantiated (or was removed), so a dependent cannot be handed an instance.

Source

Thrown at packages/dev/sharedUiComponents/src/modularTool/modularity/serviceContainer.ts:132

        const dependencies = service.consumes?.map((contract) => this._resolveDependency(contract, service)) ?? [];

        this._serviceInstances.set(service, service.factory(...dependencies));
    }

    /**
     * Resolves a dependency by contract identity for a consuming service.
     * Checks local services first, then walks up the parent chain.
     * Registers the consumer as a dependent in whichever container owns the dependency.
     * @param contract The contract identity to resolve.
     * @param consumer The service definition that consumes this dependency.
     * @returns The resolved service instance.
     */
    private _resolveDependency(contract: symbol, consumer: WeaklyTypedServiceDefinition): IService<symbol> & Partial<IDisposable> {
        const definition = this._serviceDefinitions.get(contract);
        if (definition) {
            const instance = this._serviceInstances.get(definition);
            if (!instance) {
                throw new Error(`Service '${contract.toString()}' has not been instantiated in the '${this._friendlyName}' container.`);
            }

            let dependentDefinitions = this._serviceDependents.get(definition);
            if (!dependentDefinitions) {
                this._serviceDependents.set(definition, (dependentDefinitions = new Set()));
            }
            dependentDefinitions.add(consumer);

            return instance;
        }

        if (this._parent) {
            return this._parent._resolveDependency(contract, consumer);
        }

        throw new Error(`Service '${contract.toString()}' has not been registered in the '${this._friendlyName}' container.`);
    }

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Instantiate the producing service (the container's instance-creation step) before calling dependencies() on consumers.
  2. Verify the producer and consumer go through the same container (or a correct parent chain).
  3. Check that no code removed the instance or disposed the producer before dependent resolution.
  4. Log _serviceDefinitions vs _serviceInstances contents at failure to identify the missing instantiation.

Example fix

// before
const deps = container.dependencies(consumerDef); // producer not instantiated yet
// after
await container.instantiateServices?.(); // ensure producer instances exist first
const deps = container.dependencies(consumerDef);
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the producing service is instantiated before resolving dependents
async function resolveWhenReady(container, def) {
  await container.instantiationComplete?.(); // or run instantiate step
  return container.dependencies(def);
}

Try / catch

// try {
//   return container.dependencies(def);
// } catch (e) {
//   if (String(e.message).includes("has not been instantiated")) {
//     await instantiateProducingServices(container);
//     return container.dependencies(def);
//   }
//   throw e;
// }

Prevention

When it happens

Trigger: Calling container.dependencies(definition) for a contract whose producing service is registered but not yet instantiated; resolving after the producing service's instance was removed; resolving before the container's instantiation phase ran.

Common situations: Consuming a service before the producer's create/instantiate step completed; instantiating in one container but resolving in a peer container whose _serviceDefinitions knows the contract only via different wiring; order-of-initialization bugs in module startup.

Related errors


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