not support esm syntax

Open
#45 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
30/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
node.js, typescript
Domain
tooling

Research direction

Reproduce the failure while Egg loads config/plugin.ts, then trace the require path through @eggjs/utils/dist/commonjs/import.js and the callers in @eggjs/core/src/utils/index.ts and egg_loader.ts. Determine the expected ESM behavior from the report and verify that loading a TypeScript config under a package with "type": "module" no longer raises ERR_REQUIRE_ESM.

Written by the indexing model from the issue text.

Description

package.json

 "dependencies": {
    "@eggjs/tegg": "^3.5.2",
    "@eggjs/tegg-aop-plugin": "^3.5.2",
    "@eggjs/tegg-config": "^3.2.3",
    "@eggjs/tegg-controller-plugin": "^3.5.2",
    "@eggjs/tegg-eventbus-plugin": "^3.5.2",
    "@eggjs/tegg-plugin": "^3.5.2",
    "@eggjs/tegg-schedule-plugin": "^3.5.2",
    "alova": "latest",
    "axios": "^1.6.8",
    "big.js": "latest",
    "crypto-js": "^4.2.0",
    "dayjs": "latest",
    "egg": "^4.0.10",
    "egg-redis": "^2.6.0",
    "egg-scripts": "^2.17.0",
    "egg-swagger-doc": "^2.3.2",
    "egg-tracer": "^2.0.0",
    "egg-validate": "^2.0.2",
    "lodash": "^4.17.21",
    "lodash-es": "4.17.21",
    "prom-client": "^15.1.3",
    "puppeteer": "^24.4.0",
    "skywalking-backend-js": "^0.7.0",
    "zmn-ratel-sdk-sign": "^1.0.2"
  },
  "devDependencies": {
    "@eggjs/tsconfig": "1",
    "@types/mocha": "10",
    "@types/node": "20",
    "@zmn/eslint-plugin": "latest",
    "@zmn/prettier-config": "latest",
    "@zmn/readmelint": "latest",
    "@zmn/treelint": "latest",
    "egg-bin": "6",
    "egg-mock": "^5.15.1",
    "husky": "latest",
    "jest": "latest",
    "lint-staged": "latest",
    "typescript": "5"
  },

error in terminal

2025-03-12 17:43:17,792 ERROR 39569 nodejs.Error: [@eggjs/core] load file: /Users/macbookpro/Documents/zmn/zmn-amis/packages/zmn-service/config/plugin.ts, error: Must use import to load ES Module: /Users/macbookpro/Documents/zmn/zmn-amis/packages/zmn-service/config/plugin.ts
require() of ES modules is not supported.
require() of /Users/macbookpro/Documents/zmn/zmn-amis/packages/zmn-service/config/plugin.ts from /Users/macbookpro/Documents/zmn/zmn-amis/node_modules/@eggjs/utils/dist/commonjs/import.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /Users/macbookpro/Documents/zmn/zmn-amis/packages/zmn-service/package.json.

require() of ES modules is not supported.
require() of /Users/macbookpro/Documents/zmn/zmn-amis/packages/zmn-service/config/plugin.ts from /Users/macbookpro/Documents/zmn/zmn-amis/node_modules/@eggjs/utils/dist/commonjs/import.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /Users/macbookpro/Documents/zmn/zmn-amis/packages/zmn-service/package.json.

    at Object.loadFile (/Users/macbookpro/Documents/zmn/zmn-amis/node_modules/@eggjs/core/src/utils/index.ts:92:19)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async AgentWorkerLoader.readPluginConfigs (/Users/macbookpro/Documents/zmn/zmn-amis/node_modules/@eggjs/core/src/loader/egg_loader.ts:547:22)
    at async AgentWorkerLoader.loadAppPlugins (/Users/macbookpro/Documents/zmn/zmn-amis/node_modules/@eggjs/core/src/loader/egg_loader.ts:468:24)
    at async AgentWorkerLoader.loadPlugin (/Users/macbookpro/Documents/zmn/zmn-amis/node_modules/@eggjs/core/src/loader/egg_loader.ts:415:23)
    at async AgentWorkerLoader.loadConfig (/Users/macbookpro/Documents/zmn/zmn-amis/node_modules/egg/src/lib/loader/AgentWorkerLoader.ts:12:5)
    at async Agent.loadConfig (/Users/macbookpro/Documents/zmn/zmn-amis/node_modules/egg/src/lib/egg.ts:191:5)
    at async Agent.load (/Users/macbookpro/Documents/zmn/zmn-amis/node_modules/egg/src/lib/egg.ts:195:5)
    at async /Users/macbookpro/Documents/zmn/zmn-amis/node_modules/egg/src/lib/egg.ts:179:7
cause: {"code":"ERR_REQUIRE_ESM"}
cause:
Dominant language
TypeScript
Stars
10
Forks
12
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.