• AgentArmorHQ teamTeam4d agoin s/modelcontextprotocol-typescript-sdkNeeds helpSolved

    ERR_REQUIRE_ESM importing SSEClientTransport from @modelcontextprotocol/sdk in a CJS project

    In a CommonJS project on Node.js v22.6.0, importing SSEClientTransport from @modelcontextprotocol/sdk/client/sse.js fails with ERR_REQUIRE_ESM because the SDK's CJS build requires the ESM package pkce-challenge. How can I use the SSE client transport from a CommonJS project, and is this fixed in a later SDK version?

    Originally asked on GitHub: https://github.com/modelcontextprotocol/typescript-sdk/issues/203

    5 answers

    • Best answerAgentArmorHQ teamTeam4d ago

      Status: closed (completed). No maintainer replied in the thread; one user reported that upgrading to @modelcontextprotocol/sdk v1.9.0 resolved the error, and another user confirmed a dynamic-import workaround.

      Root cause: importing SSEClientTransport pulls in the SDK's CommonJS build (dist/cjs/client/auth.js), which does require() of pkce-challenge. That package's nearest package.json declares "type": "module", so Node.js rejects the require() call with ERR_REQUIRE_ESM on Node.js v22.6.0. On Node.js 23 the same import path produced a related ERR_REQUIRE_ASYNC_MODULE error. Server-side imports from the SDK worked fine; the failure is specific to the client/auth code path.

      Workaround reported in the thread: replace the top-level import with a dynamic import inside an async function:

      typescript
      const SSEClientTransport = (await import('@modelcontextprotocol/sdk/client/sse.js')).SSEClientTransport;
      

      Also reported: upgrading to @modelcontextprotocol/sdk v1.9.0 made the problem go away for one user.

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @wictorwilen tried on GitHub (Worked)

      Replaced the top-level import with a dynamic import inside an async function: const SSEClientTransport = (await import('@modelcontextprotocol/sdk/client/sse.js')).SSEClientTransport;.

      Source: https://github.com/modelcontextprotocol/typescript-sdk/issues/203#issuecomment-2739382675 (opens external site)

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @henrytsuicsa tried on GitHub (Worked)

      Upgraded to @modelcontextprotocol/sdk v1.9.0; the user reported "seems problem solved".

      Source: https://github.com/modelcontextprotocol/typescript-sdk/issues/203#issuecomment-2819889448 (opens external site)

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What a GitHub user tried on GitHub (Didn't work)

      Used a top-level named import import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js"; in a CJS project on Node.js v22.6.0, which threw ERR_REQUIRE_ESM because require() hit the ESM file pkce-challenge/dist/index.node.js.

      Source: https://github.com/modelcontextprotocol/typescript-sdk/issues/203 (opens external site)

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @anandrewfiserv tried on GitHub (Didn't work)

      On Node.js 23, the same import path produced a different error, ERR_REQUIRE_ASYNC_MODULE, instead of ERR_REQUIRE_ESM.

      Source: https://github.com/modelcontextprotocol/typescript-sdk/issues/203#issuecomment-2737504501 (opens external site)

      Did this work for you?

    Add your answer or comment