TypeStrong / ts-node

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

not even the most simple use-case #2083

Open AndreLeifert opened 8 months ago

AndreLeifert commented 8 months ago

sudo npm i -g ts-node typescript ts-node -v v10.9.1 echo 'console.log("OK")' > tst.ts ts-node tst.ts TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/andre/play/tic/bmos-ic/tst.ts at new NodeError (node:internal/errors:405:5) at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:79:11) at defaultGetFormat (node:internal/modules/esm/get_format:124:36) at defaultLoad (node:internal/modules/esm/load:84:20) at nextLoad (node:internal/modules/esm/loader:163:28) at ESMLoader.load (node:internal/modules/esm/loader:603:26) at ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:22) at new ModuleJob (node:internal/modules/esm/module_job:64:26) at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:480:17) at ESMLoader.getModuleJob (node:internal/modules/esm/loader:434:34) { code: 'ERR_UNKNOWN_FILE_EXTENSION' }

Bjoren commented 8 months ago

Adding to this, I can't get ts-node to work at all with ESM.

A brand new project with a package.json:

{
  "type": "module",
  "dependencies": {
    "ts-node": "^10.9.1",
    "typescript": "^5.2.2"
  }
}

... and a tsconfig.json:

{
  "type": "module",
  "compilerOptions": {
    "target": "ES2022",
    "module": "ES2022"
  },
  "ts-node": {"esm": true}
}

Refuses to run script.ts

console.log("Hello world")

... with TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for <path>\project\script.ts

Very non-descriptive error message.

AndreLeifert commented 8 months ago

Yes, confirmed: like @Bjoren, I have "type": "module" in my package.json. Also tried with --esm on the command line as well as with ts-node-esm... whatever that is. Same result. Do we really have to add the suffix .js to all imports?

wolfy1339 commented 8 months ago

Duplicate of #1997