exports is not defined 与 ESM CommonJS 产物混用

March 29, 2025

2025 年 3 月 29 日,我在开发 Lite MCP SDK 时,除了遇到包子路径未公开,还在 Node.js 执行构建产物时遇到了 ReferenceError: exports is not defined。这次需要核对的是产物里的模块语法与 Node 的加载方式。1

CommonJS 代码被当成 ESM 执行

报错位置是 javascript/dist/lib/server/serverTransport.js 的第 3 行,代码行节选如下:12

Object.defineProperty(exports, "__esModule", {

这行代码使用了 CommonJS 的 exports 变量。但报错文件扩展名是 .js,所在包声明了 "type": "module",Node 因而把它作为 ES Module 加载;ES Module 中没有 CommonJS 模块包装器提供的 exports。3

这里的 exports 与 package.json 中的同名字段有不同职责:前者供 CommonJS 代码导出内容,后者控制包公开哪些入口。入口配置正确,并不能让混入 ESM 产物的 CommonJS 代码自动正常执行。3

删除报错行是当时尝试过的办法

我在 Cursor 里让模型聚焦这行 Object.defineProperty,后来又贴入通过替换插件删除它的方案,并反馈没有移除。搜索过程中,我还读过 Rollup 的 issue #2332;原帖也报错于同一行,不过它是在浏览器里加载 bundle,我则是在 Node 中测试 SDK。14

当晚提交的 babel.config.js 里还留着另一种尝试。里面的 Babel 插件会在语法树里找到以 exports 和 "__esModule" 为前两个参数的 Object.defineProperty(...) 调用,删掉整条表达式语句。对照同一提交里的产物更能说明问题:dist/index.mjs 末尾确实是 ESM 的 export { ... },而 dist/index.cjs 则通过 exports.Client 等属性导出。后者的 exports 正是 CommonJS 调用方需要的。5

删除 __esModule 标记不能普遍解决模块格式冲突。如果文件里还有 exports.someName、module.exports 或其他依赖 CommonJS 环境的语句,只删掉最先报错的一行,后面仍可能失败。这里需要检查整个产物的格式,而不是把标记本身当成根因。

分别生成两种模块产物

当天晚上,我继续问 Cursor:1

how to use roll up to package esm and cjs

当晚提交的 Rollup 配置从同一个 src/index.js 入口分别输出两种产物:6

构建格式输出文件对应的包入口条件
esmdist/index.mjsimport
cjsdist/index.cjsrequire

想看最小配置,可以参考趁你还年轻233 的 Rollup 示例。同一个 input 对应一个 output 数组,两项分别设置 format: "cjs" 和 format: "es",运行一次 rollup -c 就能生成两份文件。7

.mjs 明确表示 ESM,.cjs 明确表示 CommonJS;包的条件导出再把调用方引向相应文件。Babel 的语法转换、Rollup 的输出格式,以及 Node 根据扩展名和 type 字段选择的加载方式,需要保持一致。36

这解决的是模块格式与入口的组织方式。后来把 SDK 用进浏览器时,我又遇到了服务端依赖被一并打包的问题,另见浏览器导入 SDK 时的 raw-body 与 Buffer 错误。

参考资料

Footnotes

  1. CaiBorui. (2025, March 29). Fixing Module Import Errors in Node.js [与 Cursor 的未公开开发对话,作者留存]。下午讨论包导出入口、CommonJS 与 ESM 加载,贴入删除 __esModule 标记的方案;晚间询问用 Rollup 同时打包 ESM 和 CJS。日期按对应回复时间核对,使用北京时间。报错路径及搜索经过另据作者在 2026 年 11 月 17 日补充的记录。 ↩ ↩2 ↩3 ↩4

  2. Google. (n.d.). Package subpath 与 exports is not defined [作者保存的两次检索入口]. 包子路径未公开;exports 变量未定义。 ↩

  3. 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

  4. jeffsaremi. (2018, July 16). ReferenceError: exports is not defined [Issue #2332,原帖的构建命令与报错位置]. Rollup, GitHub. https://github.com/rollup/rollup/issues/2332 ↩

  5. CaiBorui [S2thend]. (2025, March 29). Lite MCP SDK 的 Babel 插件与两种构建产物 [版本 3026728]. GitHub. Babel 插件、ESM 产物、CommonJS 产物。 ↩

  6. CaiBorui [S2thend]. (2025, March 29). Lite MCP SDK package exports and build configuration [历史快照 3026728]. GitHub. package.json;rollup.config.js。 ↩ ↩2

  7. 趁你还年轻233. (2023, May 6). 如何打一个既支持cjs,又支持esm的npm包? [博客文章,“rollup”节的 rollup.config.js 和 package.json 示例]. CSDN. 原文。 ↩

© 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-03-29). exports is not defined 与 ESM CommonJS 产物混用. https://caiborui.com/blog/2025-03-29-zh-exports-is-not-defined-esm-cjs

@misc{borui2025-8ed667d7,
  author = {borui},
  title = {{exports is not defined 与 ESM CommonJS 产物混用}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-03-29-zh-exports-is-not-defined-esm-cjs}
}