Skip to content

ESModule 配置标准

package.json 配置

json
{
  "type": "module",
  "exports": {
    ".": {
      "types": "./dist/types/index.d.ts",
      "import": "./dist/index.js"
    }
  },
  "engines": {
    "node": ">=18.0.0"
  }
}

tsconfig.json 配置

json
{
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "target": "ES2022",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "resolveJsonModule": true
  }
}

导入规范

typescript
// 使用完整扩展名
import { util } from './util.js';

// 导入 JSON
import config from './config.json' assert { type: 'json' };

// 动态导入
const module = await import('./dynamic.js');

记住:ESM 是 JavaScript 的未来,尽早迁移。

基于 MIT 许可发布