Skip to content

fix: remove "type":"module" that conflicted with CJS lib output, causing Node.js ESM load failure#5069

Open
Copilot wants to merge 1 commit into
masterfrom
copilot/fix-module-declaration-conflict
Open

fix: remove "type":"module" that conflicted with CJS lib output, causing Node.js ESM load failure#5069
Copilot wants to merge 1 commit into
masterfrom
copilot/fix-module-declaration-conflict

Conversation

Copilot AI commented May 21, 2026

Copy link
Copy Markdown
Contributor

问题

package.json 中声明了 "type": "module",但 lib/ 目录下的构建产物是 CommonJS 格式(使用 exportsrequire())。

Node.js 根据 "type": "module" 将所有 .js 文件视为 ESM,因此尝试以 ESM 模式加载 lib/index.js 时报错:

ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension
and '@antv/x6/package.json' contains "type": "module".

关联 Issue:#5048

修复方案

移除 "type": "module"

  • "main": "lib/index.js" 指向 CJS 产物,因此无需将所有 .js 文件标记为 ESM
  • 使用 webpack/vite/rollup 等打包工具的用户仍可通过 "module": "es/index.js" 字段获得 ESM 产物
  • Node.js ESM 环境可以正常导入 CJS 模块(Node.js 会自动将 CJS 包装为 ESM 命名空间)

顺带修复:将 rollup.config.js 重命名为 rollup.config.mjs

由于 rollup.config.js 使用了 ESM 语法(import/export default),移除 "type": "module" 后需将其扩展名改为 .mjs 以继续以 ESM 模式运行。同步更新 build:umd 脚本指向新文件名。

变更内容

  • package.json:移除 "type": "module" 字段;build:umd 脚本改用 rollup.config.mjs
  • rollup.config.jsrollup.config.mjs(仅重命名,内容不变)

测试

所有现有测试通过(200 个测试文件,2990 个测试用例)。

…rename rollup.config.js to .mjs

Agent-Logs-Url: https://github.com/antvis/X6/sessions/a9e3906e-ec0d-4217-ad40-8cbc5472a8bd

Co-authored-by: lxfu1 <31396322+lxfu1@users.noreply.github.com>
@hustcc

hustcc commented May 21, 2026

Copy link
Copy Markdown
Member

@copilot 对用户来说是 break change 吗?

Copilot AI commented May 21, 2026

Copy link
Copy Markdown
Contributor Author

@copilot 对用户来说是 break change 吗?

对用户没有 breaking change。

分情况说明:

使用场景 影响
webpack / vite / rollup 等打包工具 无影响——这些工具读取 "module": "es/index.js",始终走 ESM 产物
Node.js CJS 环境(require('@antv/x6') 无影响——读取 "main": "lib/index.js",一直是 CJS
Node.js ESM 环境(import { Graph } from '@antv/x6' 报错变为正常——修复了 issue #5048 的问题

"type": "module" 本身是一个引入 breaking 行为的字段(让 Node.js 将 CJS 产物误当 ESM 加载),移除它是恢复正确行为,而非引入新的不兼容。

Copilot finished work on behalf of hustcc May 21, 2026 13:25
Copilot AI requested a review from hustcc May 21, 2026 13:25
@hustcc hustcc requested review from Copilot and removed request for hustcc and lxfu1 May 21, 2026 13:27
@hustcc hustcc marked this pull request as ready for review May 21, 2026 13:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a Node.js ESM load failure caused by declaring "type": "module" while publishing CommonJS build output under lib/, and updates the UMD build config to remain ESM-compatible after removing that package-level module type.

Changes:

  • Removed "type": "module" from package.json so Node treats lib/*.js as CommonJS.
  • Renamed the Rollup config to rollup.config.mjs and updated the build:umd script to use it explicitly.

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated no comments.

File Description
package.json Removes package-level ESM typing and updates the UMD build script to target the .mjs Rollup config.
rollup.config.mjs Provides an ESM Rollup config file compatible with running Rollup after removing "type": "module".

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants