On March 29, 2025, while developing the Lite MCP SDK, I ran into an unexported package subpath and a second error when Node.js executed the build output: ReferenceError: exports is not defined. This time I needed to check how the module syntax in the output matched the way Node loaded it.1
CommonJS code executing as ESM
The error pointed to line 3 of javascript/dist/lib/server/serverTransport.js. Here is the relevant line:12
Object.defineProperty(exports, "__esModule", {
That line uses the CommonJS exports variable. The file had a .js extension and the package declared "type": "module", so Node loaded it as an ES module. ES modules do not have the exports variable provided by the CommonJS module wrapper.3
The exports on this line serves a different purpose from the field with the same name in package.json. One lets CommonJS code export values. The other controls which package entry points are public. Correct entry points cannot make CommonJS code mixed into an ESM artifact run automatically.3
I tried deleting the offending line
In Cursor, I asked the model to focus on the Object.defineProperty line. Later, I pasted in an approach using a replacement plugin to delete it and reported that the line was still there. I also read Rollup issue #2332 during my search. The original poster hit an error on the same line while loading a bundle in a browser. I was testing the SDK in Node.14
The babel.config.js committed that evening preserves another attempt. Its Babel plugin finds Object.defineProperty(...) calls in the syntax tree whose first two arguments are exports and "__esModule", then deletes the whole expression statement. The artifacts in the same commit are more revealing. dist/index.mjs ends with an ESM export { ... }, while dist/index.cjs exposes properties such as exports.Client. That second exports is exactly what a CommonJS caller needs.5
Deleting the __esModule marker is no general solution to a module format conflict. If the file still contains exports.someName, module.exports, or other statements that require CommonJS, deleting the first line to fail may just move the error further down. The entire artifact’s format needs inspection before blaming the marker.
Producing separate artifacts for each module format
That evening I asked Cursor another question:1
how to use roll up to package esm and cjs
The Rollup configuration I committed that evening produced two outputs from the same src/index.js entry point:6
| Build format | Output file | Corresponding package entry condition |
|---|---|---|
esm | dist/index.mjs | import |
cjs | dist/index.cjs | require |
For a minimal configuration, see the Rollup example by 趁你还年轻233. The same input has an output array with two entries, one using format: "cjs" and the other format: "es". A single rollup -c invocation produces both files.7
The .mjs extension explicitly identifies ESM and .cjs explicitly identifies CommonJS. Conditional package exports direct each caller to the appropriate file. Babel’s syntax transforms, Rollup’s output format, and the loader Node selects from the extension and type field all need to agree.36
This addresses module formats and entry points. When I later used the SDK in a browser, a different problem arose: server-side dependencies were bundled too. See raw-body and Buffer errors when importing the SDK in a browser.
References
Footnotes
-
CaiBorui. (2025, March 29). Fixing Module Import Errors in Node.js [与 Cursor 的未公开开发对话,作者留存]。下午讨论包导出入口、CommonJS 与 ESM 加载,贴入删除
__esModule标记的方案;晚间询问用 Rollup 同时打包 ESM 和 CJS。日期按对应回复时间核对,使用北京时间。报错路径及搜索经过另据作者在 2026 年 11 月 17 日补充的记录。 ↩ ↩2 ↩3 ↩4 -
Google. (n.d.). Package subpath 与 exports is not defined [作者保存的两次检索入口]. 包子路径未公开;exports 变量未定义。 ↩
-
Node.js contributors. (n.d.). Modules: Packages [Node.js v22.14.0 固定版本文档,“package.json and file extensions”“Subpath exports”及“Conditional exports”]. Node.js. https://nodejs.org/download/release/v22.14.0/docs/api/packages.html ↩ ↩2 ↩3
-
jeffsaremi. (2018, July 16). ReferenceError: exports is not defined [Issue #2332,原帖的构建命令与报错位置]. Rollup, GitHub. https://github.com/rollup/rollup/issues/2332 ↩
-
CaiBorui [S2thend]. (2025, March 29). Lite MCP SDK 的 Babel 插件与两种构建产物 [版本
3026728]. GitHub. Babel 插件、ESM 产物、CommonJS 产物。 ↩ -
CaiBorui [S2thend]. (2025, March 29). Lite MCP SDK package exports and build configuration [历史快照
3026728]. GitHub. package.json;rollup.config.js。 ↩ ↩2 -
趁你还年轻233. (2023, May 6). 如何打一个既支持cjs,又支持esm的npm包? [博客文章,“rollup”节的
rollup.config.js和package.json示例]. CSDN. 原文。 ↩