ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

Test REPL in nodenext type=module projects; may need a tweak to ensure REPL is CJS

Open cspotcode opened this issue 3 years ago • 1 comments
trafficstars

While working on #1694, stumbled upon a possible REPL issue: if package.json type = module, and you fire up the REPL, how is the REPL being compiled? Should make sure it's treated as CJS.

Should also check ts-node -pe "is this code ESM or CJS? What should it be?"

cspotcode avatar May 17 '22 02:05 cspotcode

The fix here might be as simple as either:

A) naming the repl's "virtual file" to end in the .cts file extension B) using the moduleTypes option internally to override the repl file to be CJS.

Internally, the repl is a virtual .ts file that exists solely in memory. This allows us to typecheck it like any other file. Since it already has a name, we can give it the cts file extension and get the same benefits.

If we go the cts route, we will need to be sure this works even without module=nodenext. We will also need to confirm which TSC versions support cts. It may be acceptable to use cts when the user's TSC version is new enough, and gracefully fallback to legacy repl behavior otherwise. Users with ESM projects are likely already on a new TS version, so we solve the problem for most users who would encounter it.

cspotcode avatar Jun 10 '22 04:06 cspotcode

Not sure, if I understand the solution correctly. But, if you are recommending to always used CommonJS, then shouldn't that be wrong?

If my project is in ESM, then the REPL should be ESM too?

thetutlage avatar Nov 07 '22 17:11 thetutlage

The question is: what does node's REPL do? We'll match that.

cspotcode avatar Nov 07 '22 17:11 cspotcode

REPL doesn't support ESM: https://github.com/nodejs/node/issues/33369

GabenGar avatar Nov 14 '22 11:11 GabenGar