TypeStrong / ts-node

TypeScript execution and REPL for node.js
https://typestrong.org/ts-node
MIT License
12.75k stars 529 forks source link

[Error [ERR_REQUIRE_ESM]: require() of ES Module /.../node_modules/ts-node/esm.mjs not supported. #2115

Closed darcyrush closed 2 months ago

darcyrush commented 2 months ago

Search Terms

ERR_REQUIRE_ESM esm.mjs not supported. [Error [ERR_REQUIRE_ESM]: require() of ES Module /.../node_modules/ts-node/esm.mjs not supported.

Expected Behavior

I am trying to run node using ts-node/esm and the latest node implementation of --require rather than --loader.

Actual Behavior

Error below

> test
> TS_NODE_PROJECT=./test/tsconfig.json node --require ts-node/esm --test 'test/main.test.ts'

node:internal/modules/cjs/loader:1262
    throw new ERR_REQUIRE_ESM(filename, true);
    ^

[Error [ERR_REQUIRE_ESM]: require() of ES Module /.../node_modules/ts-node/esm.mjs not supported.
Instead change the require of /.../node_modules/ts-node/esm.mjs to a dynamic import() which is available in all CommonJS modules.] {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v22.0.0

Steps to reproduce the problem

npm install
npm run test       # Error
npm run build      # To see that `tsc` compiles fine

Minimal reproduction

Minimal reproduction repository; https://github.com/darcyrush/ts-node-test-err-require-esm

Specifications

darcyrush commented 2 months ago

Cannot RTFM, --import instead of --require. I get the widely documented Unknown file extension error