Skip to content

Export ts-node/esm-register#2073

Open
jlenon7 wants to merge 4 commits into
TypeStrong:mainfrom
jlenon7:ts-node/esm-register
Open

Export ts-node/esm-register#2073
jlenon7 wants to merge 4 commits into
TypeStrong:mainfrom
jlenon7:ts-node/esm-register

Conversation

@jlenon7

@jlenon7 jlenon7 commented Oct 4, 2023

Copy link
Copy Markdown

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

klippx commented Oct 12, 2023

Copy link
Copy Markdown

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
Copy Markdown
Contributor

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

@kkolinko

kkolinko commented Nov 15, 2023

Copy link
Copy Markdown

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.

Comment thread esm-register.mjs Outdated
@ihmpavel

ihmpavel commented Jan 5, 2024

Copy link
Copy Markdown

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

@nickserv

nickserv commented Jan 5, 2024

Copy link
Copy Markdown

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

jlenon7 commented Jan 7, 2024

Copy link
Copy Markdown
Author

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

@AaronNGray

AaronNGray commented Jan 16, 2024

Copy link
Copy Markdown

For further context and alternative example see :- #2100

@ElvenMonky

Copy link
Copy Markdown

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

@jlenon7

jlenon7 commented May 5, 2024

Copy link
Copy Markdown
Author

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.

@parimalyeole1

Copy link
Copy Markdown

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

Thank You

@eduwardo

eduwardo commented Jun 12, 2024

Copy link
Copy Markdown

Any update on merging this pr?

@albertodiazdorado

Copy link
Copy Markdown

It would be lovely if we could get this merged

@jlenon7

jlenon7 commented Jul 16, 2024

Copy link
Copy Markdown
Author

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

@programmingwithsalim

Copy link
Copy Markdown

npm i -D tsx
npx tsx src/index.ts

This worked for me

@albertodiazdorado

Copy link
Copy Markdown

I agree with @programmingwithsalim . Either tsx (based on esbuild, written in go) or swc-node (based on swc, written in rust) are the standard way to directly execute TypeScript files in 2025.

Sadly, ts-node is a relic of the past.

(Additionally: we shall keep an eye on the built-in feature of Node.js to execute TypeScript files, which is also based on swc if I am not mistaken)

domq added a commit to epfl-si/confluence2outline that referenced this pull request May 21, 2025
```bash
npm i --save-dev typescript @types/node tsx
```

🗞️ Newsflash: [ts-node is
dead](TypeStrong/ts-node#2073 (comment)).
(Indeed I had ample opportunity to confirm by myself that `ts-node`,
`ts-mocha` et al hardly work at all with modern Node, what with the
latter “helpfully” trying to parse the TypeScript by themselves, and
failing miserably when you try to import a type.)
@davidthornton

Copy link
Copy Markdown

@albertodiazdorado, not to mention the speed benefits - which we've all heard about but I can anecdotally confirm have indeed been picked up for us too when running node --watch, node --test and when starting up in production! Very sad for ts-node, but very exciting for swc-node and TypeScript and the world! Do you have a reference to the built-in TypeScript running feature planning from Node.js? I would like to take a look. Thanks!

@inoyakaigor

Copy link
Copy Markdown

@davidthornton I think this link could help you to start https://nodejs.org/docs/latest-v22.x/api/typescript.html#type-stripping

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.