BabylonJS/Babylon.js · error · Error

Function not injected yet. Use the factory to create the plu

Error message

Function not injected yet. Use the factory to create the plugin.

What it means

RecastNavigationJSPluginV2.createNavMesh (RecastNavigationJSPlugin.ts:191) throws this when createNavMeshImpl is undefined. The plugin class deliberately ships with no generator implementation; the factory functions (CreateNavigationPluginAsync, etc.) construct the plugin and inject the generator functions. Calling createNavMesh on a plugin you constructed yourself with `new RecastNavigationJSPluginV2()` (without a RecastInjection, which triggers GetRecast/InjectGenerators, or before initialization completed) leaves the impl unassigned and the call fails.

Source

Thrown at packages/dev/addons/src/navigation/plugin/RecastNavigationJSPlugin.ts:191

    /**
     * Get the time factor used for crowd agent update
     * @returns the time factor
     */
    public get timeFactor(): number {
        return this._timeFactor;
    }

    /**
     * Creates a navigation mesh - will be injected by the factory
     * @param meshes array of all the geometry used to compute the navigation mesh
     * @param parameters bunch of parameters used to filter geometry
     * @returns the created navmesh and navmesh query
     * @throws Error if the function is not injected yet or if the navmesh is not created
     */
    public createNavMesh(meshes: Array<Mesh>, parameters: INavMeshParametersV2): CreateNavMeshResult {
        if (!this.createNavMeshImpl) {
            throw new Error("Function not injected yet. Use the factory to create the plugin.");
        }

        this._preprocessParameters(parameters);

        const result = this.createNavMeshImpl(meshes, parameters);
        return this._processNavMeshResult(result);
    }

    /**
     * Creates a navigation mesh asynchronously - will be injected by the factory
     * @param meshes array of all the geometry used to compute the navigation mesh
     * @param parameters bunch of parameters used to filter geometry
     * @returns the created navmesh and navmesh query
     * @throws Error if the function is not injected yet or if the navmesh is not created
     */
    public async createNavMeshAsync(meshes: Array<Mesh>, parameters: INavMeshParametersV2): Promise<CreateNavMeshResult> {
        if (!this.createNavMeshAsyncImpl) {
            throw new Error("Function not injected yet. Use the factory to create the plugin.");

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Create the plugin through the factory: `const plugin = await CreateNavigationPluginAsync();` instead of `new RecastNavigationJSPluginV2()`.
  2. Ensure the factory promise is awaited before any createNavMesh call.
  3. If you must construct manually, pass a proper RecastInjection (so GetRecast + InjectGenerators run) and confirm recast wasm is initialized first (await init()).
  4. In worker mode, use createNavMeshAsync — sync createNavMesh is intentionally disabled there.

Example fix

// before
const plugin = new RecastNavigationJSPluginV2();
plugin.createNavMesh(meshes, params); // throws: impl not injected

// after
const plugin = await CreateNavigationPluginAsync();
plugin.createNavMesh(meshes, params);
Defensive patterns

Strategy: validation

Validate before calling

// Run BEFORE calling createNavMesh
if (!plugin.createNavMeshImpl) {
    throw new Error("Plugin not created via factory. Use: const plugin = await CreateNavigationPluginAsync()");
}

Type guard

function isFactoryCreated(plugin: RecastNavigationJSPluginV2): boolean {
    return typeof plugin.createNavMeshImpl === "function";
}

Try / catch

try {
    plugin.createNavMesh(meshes, params);
} catch (e) {
    if (e instanceof Error && e.message.includes("Function not injected yet")) {
        throw new Error("Construct the plugin with CreateNavigationPluginAsync(), not `new RecastNavigationJSPluginV2()`");
    }
    throw e;
}

Prevention

When it happens

Trigger: Instantiating the plugin directly via `new RecastNavigationJSPluginV2()` and then calling createNavMesh, instead of using CreateNavigationPluginAsync()/CreateNavigationPluginWorkerAsync(); or calling createNavMesh before the async factory's promise resolved; or constructing with `new RecastNavigationJSPluginV2(customInjection)` where the injection did not run InjectGenerators. Note the worker-mode plugin overrides createNavMesh to warn and return null, so this throw mainly applies to manually constructed plugins.

Common situations: Migrating from the old RecastJSPlugin where direct construction was normal; forgetting to await the factory; bundler tree-shaking the factory module; calling the sync method on a worker-mode plugin mindset.

Related errors


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