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

52 lines
799 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ESM vs CommonJS 快速参考
## 问题症状
```bash
Error [ERR_REQUIRE_ESM]: require() of ES Module not supported
```
## 原因
某些包(如 chalk 5+)只支持 ES Modules无法在 CommonJS 项目中使用 `require()` 导入。
## 解决方案
### 1. 降级到兼容版本 ⭐ 推荐
```json
{
"dependencies": {
"chalk": "^4.1.2" // 而不是 ^5.4.1
}
}
```
### 2. 转换为 ESM 项目
```json
{
"type": "module"
}
```
### 3. 动态导入
```javascript
// 替代 const chalk = require('chalk');
const chalk = await import('chalk');
console.log(chalk.default.red('text'));
```
## 检查包的模块类型
```bash
npm info <package-name>
```
查看 `"type": "module"` 字段。
## 详细文档
参见 [Node.js 模块系统详解](./nodejs-module-systems-guide.md)