Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export ts-node/esm-register #2073

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jlenon7
Copy link

@jlenon7 jlenon7 commented Oct 4, 2023

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

See issue #2072

The `--loader` flag might be removed in future version.
With `ts-node/esm-register` users will be able to register the
loader using the `--import` flag.
@klippx
Copy link

klippx commented Oct 12, 2023

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

@TheUnlocked
Copy link
Contributor

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

@kkolinko
Copy link

kkolinko commented Nov 15, 2023

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.

esm-register.mjs Outdated Show resolved Hide resolved
@ihmpavel
Copy link

ihmpavel commented Jan 5, 2024

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

@nickmccurdy
Copy link

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?

@jlenon7
Copy link
Author

jlenon7 commented Jan 7, 2024

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

@AaronNGray
Copy link

AaronNGray commented Jan 16, 2024

For further context and alternative example see :- #2100

@ElvenMonky
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants