TypeStrong / ts-node

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

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

Open cspotcode opened 2 years ago

cspotcode commented 2 years ago

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 commented 2 years ago

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.

thetutlage commented 1 year ago

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?

cspotcode commented 1 year ago

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

GabenGar commented 1 year ago

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