mcp-swagger-server/docs/esm-commonjs-quick-referenc...

799 B
Raw Blame History

ESM vs CommonJS 快速参考

问题症状

Error [ERR_REQUIRE_ESM]: require() of ES Module not supported

原因

某些包(如 chalk 5+)只支持 ES Modules无法在 CommonJS 项目中使用 require() 导入。

解决方案

1. 降级到兼容版本 推荐

{
  "dependencies": {
    "chalk": "^4.1.2"  // 而不是 ^5.4.1
  }
}

2. 转换为 ESM 项目

{
  "type": "module"
}

3. 动态导入

// 替代 const chalk = require('chalk');
const chalk = await import('chalk');
console.log(chalk.default.red('text'));

检查包的模块类型

npm info <package-name>

查看 "type": "module" 字段。

详细文档

参见 Node.js 模块系统详解