Skip to content

Exception during run: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: #5504

@khteh

Description

@khteh

I don't have any require() in my code but still hit the following error:

$ ./node_modules/.bin/mocha -r ts-node/register tests/**/*.ts --reporter mocha-junit-reporter --reporter-options mochaFile=test_reports/mocha/test-results.xml --exit
(node:1015864) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));'
(Use `node --trace-warnings ...` to show where the warning was created)
(node:1015864) [DEP0180] DeprecationWarning: fs.Stats constructor is deprecated.
(Use `node --trace-deprecation ...` to show where the warning was created)

 Exception during run: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /usr/src/Node.JS/Node.JSRestAPI/tests/IntegrationTests/add_students_to_teacher_tests.ts
require() of ES modules is not supported.
require() of /usr/src/Node.JS/Node.JSRestAPI/tests/IntegrationTests/add_students_to_teacher_tests.ts from /usr/src/Node.JS/Node.JSRestAPI/node_modules/mocha/lib/nodejs/esm-utils.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 /usr/src/Node.JS/Node.JSRestAPI/package.json.

    at createErrRequireEsm (/usr/src/Node.JS/Node.JSRestAPI/node_modules/ts-node/dist-raw/node-internal-errors.js:46:15)
    at assertScriptCanLoadAsCJSImpl (/usr/src/Node.JS/Node.JSRestAPI/node_modules/ts-node/dist-raw/node-internal-modules-cjs-loader.js:584:11)
    at Object.require.extensions.<computed> [as .ts] (/usr/src/Node.JS/Node.JSRestAPI/node_modules/ts-node/src/index.ts:1610:5)
    at Module.load (node:internal/modules/cjs/loader:1479:32)
    at Module._load (node:internal/modules/cjs/loader:1298:12)
    at TracingChannel.traceSync (node:diagnostics_channel:328:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:244:24)
    at Module.require (node:internal/modules/cjs/loader:1502:12)
    at require (node:internal/modules/helpers:152:16)
    at Object.requireModule [as requireOrImport] (/usr/src/Node.JS/Node.JSRestAPI/node_modules/mocha/lib/nodejs/esm-utils.js:98:12)
    at Object.exports.loadFilesAsync (/usr/src/Node.JS/Node.JSRestAPI/node_modules/mocha/lib/nodejs/esm-utils.js:152:34)
    at Mocha.loadFilesAsync (/usr/src/Node.JS/Node.JSRestAPI/node_modules/mocha/lib/mocha.js:429:19)
    at singleRun (/usr/src/Node.JS/Node.JSRestAPI/node_modules/mocha/lib/cli/run-helpers.js:168:15)
    at exports.runMocha (/usr/src/Node.JS/Node.JSRestAPI/node_modules/mocha/lib/cli/run-helpers.js:245:10)
    at Object.exports.handler (/usr/src/Node.JS/Node.JSRestAPI/node_modules/mocha/lib/cli/run.js:379:11)
    at /usr/src/Node.JS/Node.JSRestAPI/node_modules/yargs/build/index.cjs:1:8993
    at /usr/src/Node.JS/Node.JSRestAPI/node_modules/yargs/build/index.cjs:1:4949 {
  code: 'ERR_REQUIRE_ESM'
}
node v25.0.0
"typescript": "^5.9.3"
"mocha": "^11.7.4",

package.json:

"type": "module",

tsconfig.json:

  "node-option": [
    "loader=ts-node/esm"
  ],
 "compilerOptions": {
      "target": "ES2022",
      "module": "nodenext", 
      "moduleResolution": "nodenext",
      "types": ["mocha"],
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions