raw-body and Buffer errors when importing an SDK in the browser

April 2, 2025

On April 2, 2025, I hit Cannot read properties of undefined (reading 'prototype') while integrating the Lite MCP SDK into the PromptPulse frontend. It failed during SDK import. The stack trace included dependencies used on the server to read HTTP request bodies.1

Following the stack trace to a server-side dependency

The stack trace I pasted into Cursor included safer-buffer, iconv-lite, and raw-body. I then pasted SDK code starting with import getRawBody from 'raw-body' that exported both client and server. I asked how to get it working, then followed up:1

how can i implement a version can run on both broser and nodejs env

I also searched for the raw-body package. In the SDK it read a POST request stream into text before passing it to JSON.parse() and the protocol message handler. Cursor’s explanation of the server transport classes on March 26 had already mentioned the package and the parsedBody parameter for accepting a body previously parsed by middleware. On April 2 I ran into the runtime compatibility problem myself.12

The SDK’s single entry point exported Client, Server, and both transports. The browser only needed the client, but importing the SDK also pulled in raw-body, used by SSEServerTransport, and its dependencies. That matched the stack trace: even though the application only intended to use the client, the entry point’s dependency graph already included server-side code.3

Providing a separate browser entry point

Before selecting an entry point for the browser, I had fixed another easily confused path. At 14:12 on April 2 I changed the root entries of the upper-level litemcpjs package from dist/index.mjs and dist/index.cjs to dist/litemcp.mjs and dist/litemcp.cjs, the files actually configured in Rollup. At 18:19 the stack trace I shared was already reaching safer-buffer. Even when the package entry selects the right file, that file can still bring server dependencies into the frontend.41

On April 3 I added a browser entry point to the underlying SDK. It imports and exports just the client and its transport.3

import { Client } from "./lib/client/client.js";
import { SSEClientTransport } from "./lib/client/clientTransport.js";

export { Client, SSEClientTransport };

If the frontend and Node.js both import the SDK by package name, how does the bundler choose a file? A Stack Overflow question used main for the server file and browser for the client file.5 When targeting a browser, bundlers supporting the browser convention can replace the default entry point using a string value or replace specific files using mappings in an object.6

In the SDK’s package.json, I used an object to map ./dist/index.mjs to ./dist/browser.mjs. The upper-level litemcpjs needed its own mapping because its usual entry also exported a server initialization function. The commit at 17:03 mapped ./dist/litemcp.mjs to ./dist/litemcp_broswer.mjs, which exported only MCPClient (the filename retains its original spelling). That evening the PromptPulse frontend imported MCPClient by package name, while the backend registered Puppeteer tools with MCPServerInit.37

The split aligned the browser entry point’s dependencies with its actual use. The client can share protocol message handling without importing server modules that handle Node HTTP requests. It also makes the build easier to inspect: start at the browser entry, follow its imports, and check that every dependency works in the target environment.

How this differs from an ESM or CommonJS error

The exports is not defined error I encountered a few days earlier required matching the module format to the way it was loaded. Here, even matching module syntax could still leave the runtime incompatible. When troubleshooting an SDK import, check the package entry point and module format, then inspect which platform dependencies the entry actually brings in.

References

Footnotes

  1. CaiBorui. (2025, March 26; April 2). Summary of SseClientTransport Class Relationships; Module Import Error Troubleshooting [Unpublished development conversations with Cursor, retained by the author]. On March 26 I asked for an explanation of the server transport classes. On April 2 I pasted the raw-body stack trace and SDK code and asked about a shared browser and Node.js implementation. The original English question retains its spelling. Individual user messages were dated against the immediately following replies, in Beijing time. I also pasted an error from the same dependency chain on April 2 in a separate session, JavaScript to Python Code Translation. ↩ ↩2 ↩3 ↩4

  2. npm. (n.d.). raw-body [Package search saved by the author]. npm. https://www.npmjs.com/search?q=raw-body . For the request body reader, see Lite MCP SDK serverTransport.js, historical snapshot 3026728. ↩

  3. CaiBorui [S2thend]. (2025, April 3). Lite MCP SDK browser entry changes [Commits]. GitHub. Underlying browser entry 5b43fb2, Upper-level browser entry 8622627. For dependencies in that version, see index.js and serverTransport.js. ↩ ↩2 ↩3

  4. CaiBorui [S2thend]. (2025, April 2). Fix package export path [litemcpjs 0.1.2]. GitHub. Entry-point filename changes. The local Cursor conversation places the Buffer error that evening. Firefox history records the Q&A in note 5 at 18:30:02 and the field specification in note 6 at 18:30:29, all Beijing time. ↩

  5. Everettss. (2016, June 27). Different main entry point in package.json for node and browser [Question]. Stack Overflow. The question’s package.json example. William Swanson’s answer of March 10, 2018 links to the browser field specification. ↩

  6. defunctzombie. (n.d.). package-browser-field-spec. GitHub. Alternative entry as a string and File mapping as an object. ↩

  7. CaiBorui [S2thend]. (2025, April 3). PromptPulse migration to litemcpjs [Commits]. GitHub. Frontend migration ec8c6c2, Backend migration daa0c72. See also PromptPulse development notes: choosing Electron and packaging local models. ↩

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-04-02). raw-body and Buffer errors when importing an SDK in the browser. https://caiborui.com/blog/2025-04-02-zh-browser-import-node-dependencies-entrans

@misc{borui2025-040f67f7,
  author = {borui},
  title = {{raw-body and Buffer errors when importing an SDK in the browser}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-04-02-zh-browser-import-node-dependencies-entrans}
}