#tsc transpiles a script but ts-node fails to run it

6 messages · Page 1 of 1 (latest)

onyx tree
#

Hi folks. I've got a question regarding how ts-node internally transpiles TypeScript during execution.

I've got the following code in a .ts script:

import { publicKeyVerify } from "secp256k1"

Transpiling this code using tsc works normally. However, running ts-node (via node -t ts-node/register --loader ts-node/esm and other such incantations) does not work -- it gives me an error:

The requested module 'secp256k1' is a CommonJS module, which may not support all module.exports as named exports.

I understand how to fix this -- by doing an import such as:

import secp256k1 from "secp256k1"
const { publicKeyVerify } = secp256k1

My question is: why does tsc work perfectly fine, and ts-node does not, given that (from what I gather) ts-node uses tsc internally as a transpiler?

It kinda bothers me the fact that both tools do not agree on what syntax is necessary, when I would suppose they should -- after all, ts-node transpiles using the same exact compiler and same exact configuration.

I've also posted a question to StackOverflow, in case anyone is interested on answering there: https://stackoverflow.com/q/79895491/259543

Any help is appreciated!

fresh stratus
#

@onyx tree ts-node never really got full support for ESM stuff; if you want a ts runner, tsx is generally the more modern alternative.

onyx tree
#

@fresh stratus Thanks. Actually the same issue happens with tsx as well. The error message is a bit different but both cause/solution are the same.

#

What puzzles me is that tsc groks the code, but no REPL/runner does, it doesn't make much sense honestly, but I don't know how these runners really integrate with Node...

fresh stratus
#

You might need to check your tsc settings - if you've got it set to something permissive like moduleResolution: "bundler" it's not going to properly validate node behavior at type-checking

onyx tree
#
"module": "nodenext",
"moduleResolution": "nodenext",
"target": "esnext"