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

Export `ts-node/esm-register`

Open jlenon7 opened this issue 2 years ago • 17 comments

Register ts-node/esm loader programmatically using register fn.

See issue #2072

jlenon7 avatar Oct 04 '23 15:10 jlenon7

This is great, we will get rid of

(node:2649) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./")); register("extensionless", pathToFileURL("./"));'

if we use the --import ts-node/esm instead of --loader ts-node/esm for Node 20.x projects

klippx avatar Oct 12 '23 18:10 klippx

Make sure to add esm-register.mjs to the files list in package.json.

TheUnlocked avatar Oct 26 '23 17:10 TheUnlocked

Regarding the proposed patch:

  1. The first argument to register(): What is the point of calling pathToFileURL() on it? Why one would want to convert a relative path to a file:// URL?

    (I would understand if it were an absolute path, resulting in an absolute URL. And in that case the second argument to register() could be omitted. But why for a relative one? register() is able to resolve relative paths relative to its second argument - that is the purpose of the second argument of register().)

  2. The second argument to register(): What is the meaning of "./" here? It usually means the current working directory of a process. Why not use import.meta.url - the URL of this very module?

    (In the ExperimentalWarning, cited by klippx in a comment above, pathToFileURL("./") is used because it is inline code used on the command line. There were no module file, and "./" had a meaning in that case.)

An example can be found here: https://nodejs.org/dist/latest-v20.x/docs/api/module.html#enabling Click "CJS <-> ESM" switch in the top-right corner to show it in ESM syntax.

kkolinko avatar Nov 15 '23 12:11 kkolinko

Hi @jlenon7 did you have time to look at the proposed changes? Looking forward to this PR to be merged. If I can help, i would love to

ihmpavel avatar Jan 05 '24 14:01 ihmpavel

This looks good and it's nearly identical to the code Node recommends when using legacy --loader, though I'd recommend @thetutlage's suggestion. Is this enough to support --import ts-node/esm or would it have to be exposed?

nickserv avatar Jan 05 '24 18:01 nickserv

Sorry the delay, I've uploaded the code with the suggestions.

jlenon7 avatar Jan 07 '24 12:01 jlenon7

For further context and alternative example see :- https://github.com/TypeStrong/ts-node/issues/2100

AaronNGray avatar Jan 16 '24 20:01 AaronNGray

So what's the resolution on this? Is it coming in?

ElvenMonky avatar May 04 '24 01:05 ElvenMonky

So what's the resolution on this? Is it coming in?

Well, the implementation is done. We just need to wait to the core contributors to merge it or some feedback if they need more changes.

jlenon7 avatar May 05 '24 17:05 jlenon7

@jlenon7 Thanks for this PR. @blakeembrey @thetutlage, Is it possible to move forward with this PR? This will help us a lot.

Thank You

parimalyeole1 avatar May 09 '24 12:05 parimalyeole1

Any update on merging this pr?

eduwardo avatar Jun 12 '24 19:06 eduwardo

It would be lovely if we could get this merged

albertodiazdorado avatar Jul 01 '24 08:07 albertodiazdorado

Hey @cspotcode could you take a look on this please?

jlenon7 avatar Jul 16 '24 13:07 jlenon7