Skip to content

Initializing Humanloop SDK fails with SemVer TypeError in Cloudflare Worker #10

Open

Description

@HananAlsawy

Description:

When initializing Humanloop SDK in a Cloudflare Worker, I encounter the following error:
[ERROR] service core:user:: Uncaught TypeError: Invalid Version: in SemVer

Environment:

  • pnpm version: 10.0.0
  • node version: 20.16.0
  • humanloop version: ^0.8.10
  • Runtime: Cloudflare Worker

Steps to Reproduce:

1- Install Humanloop SDK in a Cloudflare Worker
2- Initialize the SDK
3- Observe the error in the logs

Steps Taken to Resolve:

  • Deleted node_modules, pnpm-lock.yaml, pnpm cache and reinstalled dependencies

Screenshot:

Image

Any guidance on resolving this issue would be appreciated. Let me know if you need any further details.

Activity

  1. jamesbaskerville commented on Feb 19, 2025

    @jamesbaskerville
    Contributor

    Hi @HananAlsawy, after a little digging I've found this occurs because @opentelemetry/sdk-trace-node checks against process.version, which doesn't exist in the CF Worker runtime. We'll consider supporting Cloudflare Workers better, but I can't promise anything soon.

    For now, I was able to get around this constraint by patching the globalThis and assigning a mock version to process. Hopefully unnecessary once open-telemetry/opentelemetry-js#5306 is released.

    You can try something similar to the following in your worker def and let me know if it works.

    import { HumanloopClient } from "humanloop";
    type Env = {
    	HUMANLOOP_API_KEY: string;
    }
    
    export default {
    	async fetch(request, env, ctx): Promise<Response> {
    		// Hack to fix @opentelemetry/sdk-trace-node
    		// Create a mock process object if it doesn't exist
    		const globalProcess = (globalThis as any).process || {};
    		// Set the version property if it doesn't exist
    		if (!globalProcess.version) {
    		  globalProcess.version = '16.0.0';
    		}
    		// Assign the mock process object to global scope
    		(globalThis as any).process = globalProcess;
    
    		const client = new HumanloopClient({ apiKey: env.HUMANLOOP_API_KEY });
    		return new Response(JSON.stringify(await client.directories.list()));
    	},
    } satisfies ExportedHandler<Env>;
  2. yorjaggy commented on Feb 24, 2025

    @yorjaggy

    I was having the same issue, thanks for that workaround @jamesbaskerville it works for me 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions