1

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:

enter image description here

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" }.

1 Answer 1

1
+50

Use the full path, e.g.,

import {things} from "folder/file.ts" things() 

Use ts-node-esm.

Add to tsconfig.json "allowImportingTsExtensions": true,
(I think it's also fine to add to test.json).

Not sure if needed but also try to add to main tsconfig.json: "module": "ESNext"

For top level await issue package.json should have "type": "module".

And if there's no special reason to use ES2022 use ESNext in all files.

Edit: Apparently it works with node v19 and down. Tried with 17,18,19, and it worked, in 20,21,22 not.

Edit2: This works also in new versions -

{ "compilerOptions": { "allowImportingTsExtensions": true, // works also with ES2022 and NodeNext // with ES* no need to specify the whole path "module": "ESNext", path "noEmit": true } } 

With the command node --loader ts-node/esm test.ts

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for the answer. I am appreciate of your time, but please, do not recommend what already has been done at the moment of question asking (specifying of full path to module, specifying "module": "ESNext" etc.). I am sorry, but would you please to update your answer? Also, there was not initially, but I have added the explanation why { "type": "module" }. will not work.
I've checked the mixes of config available, what's work's is if you use all of them together. I saw the issue of ts extension and using mentioned configs together solve it. About common and and es, i had similar problem in a library that we want to supply to both standard users - you can look our config github.com/valkey-io/valkey-glide/blob/…
For top level await, adding ts-node configs to tsconfig with experimental-repl-await on may help. You changed the question few time, when i first answer it was on tsnode and module issues, the answers may loose relevance after each question change.
Thank you for the support of your answer. Unfortunately, because of complexity of this problem I can't say that its exhausted. But for your efforts, I have given you the reputation prize and upvoted your question.
Thanks. If the issue still exist i would be happy to further try to solve it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.