First of all, looks like this problem is complex. Besides IntelliJ IDEA and ts-node, it could be the problems with TypeScript and tsx (looks like this topic is related). Currently I am not sure on which side the bug is, so I decided the title based on what I actually see, but looks like it is just iceberg apex. Anyway, I suppose, we can not consider everything is single question.
Target TypeScript file
This issue is actual for all files below Tests/Automated (and, apparently, not only), but I'll take Tests/Automated/Sample.test.ts file for example.
Main tsconfig.json
This configuration file does not cover Tests/Automated/Sample.test.ts:
{ "compilerOptions": { "target": "ES2022", "moduleResolution": "Node", "esModuleInterop": true, "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, "noImplicitReturns": true, "skipLibCheck": true, "removeComments": true, "outDir": "Distributable/", "declaration": true }, "include": [ "Source/**/*" ] } tsconfig.test.json - configuration for test files
This files is expected to cover the Tests/Automated/Sample.test.ts:
{ "extends": "./tsconfig.json", "compilerOptions": { "target": "ESNext", "module": "ESNext" }, "include": [ "Tests/**/*" ] } Problem
IntelliJ IDEA complains that module and target options are wrong:
TS1378: Top-level await expressions are only allowed when the module option is set to es2022, esnext, system, node16, nodenext, or preserve, and the target option is set to es2017 or higher.
Same error will be if try to run
ts-node Tests/Automated/Sample.test.ts -p CoreLibrary/Package/tsconfig.test.json { "type": "module" } is NOT the solution! (even not only of this problem)
If to add { "type": "module" } to package.json, the error will change to
TypeError: Unknown file extension ".ts" for C:\XXX\Tests\Automated\Sample.test.ts In recent years, the adding of { "type": "module" } is frequently being recommended to solve the similar problems. From my experience, it never helped, just caused another problems instead of previous ones. One of the reasons is node_modules directory could include both ES Modules and CommonJS modules, so we need the solution where the utility compiling and/or executing the TypeScript code (ts-node, tsx, webpack, etc.) could handle both ES Modules and CommonJS modules without adding of { "type": "module" }.
