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

ts-node cannot transpile on nodeJS 20 #1246

Closed
wataruian opened this issue Jan 11, 2024 · 5 comments
Closed

ts-node cannot transpile on nodeJS 20 #1246

wataruian opened this issue Jan 11, 2024 · 5 comments

Comments

@wataruian
Copy link

Do you want to request a feature or report a bug?

I'm not sure if this is intended behavior or a bug

What is the current behavior?

When using ts-node with oclif, warning appears and the existing compiled source is executed instead
Warning: ts-node executable cannot transpile ESM in Node 20. Existing compiled source will be used instead.

This affects the development experience because you will need to compile in order to test existing code.

Will it be possible to use tsx instead of ts-node?

What is the expected behavior?

Able to transpile code in dev mode.

"@oclif/core": "^3.15.1"
Node: v20.10.0

@mdonnalley
Copy link
Contributor

@wataruian Yep, this a warning we intentionally emit here

The TL;DR of it is that Node changed some things in v20 that broke how ts-node's esm loader was working. Because of that, we're not able to compile ESM at runtime when using node 20.

However it is possible to use tsx. You just need to replace the shebang in your bin/dev.js from ts-node to tsx
and everything should still work as expected. If you find any odd behavior using tsx - feel free to post a new issue or in this PR that's been hanging around for a while.

Here's a reading list in case you're curious:

If it's okay with you, I'm going to close this issue since we already have an issue about ts-node + ESM + node 20 and there's a viable workaround - but please reopen it if you feel otherwise. Thanks!

@mdonnalley
Copy link
Contributor

mdonnalley commented Jan 11, 2024

Just realized that the original oclif/core issue about this was closed. So I'm going to keep this one open and pin it since others are likely to run into this

@mdonnalley mdonnalley reopened this Jan 11, 2024
@mdonnalley mdonnalley pinned this issue Jan 11, 2024
@wataruian
Copy link
Author

wataruian commented Jan 13, 2024

@mdonnalley, I am not sure how to make tsx work.

I tried to replace dev.js shebang with #!/usr/bin/env -S node --import tsx --no-warnings=ExperimentalWarning then I invoked with: yarn node bin/dev.js hello, I am getting error:

(node:50370) [ERR_UNKNOWN_FILE_EXTENSION] TypeError Plugin: my-cli: Unknown file extension ".ts" for /Users/wataruian/sample/packages/my-cli/src/commands/hello.ts

If I tried to invoke with yarn node --import tsx bin/dev.js hello, I am getting error:

node:internal/process/esm_loader:40
      internalBinding('errors').triggerUncaughtException(
                                ^
Error: Parse error @:4:49
    at Ge (file:///Users/wataruian/sample/.yarn/cache/tsx-npm-4.7.0-86d7b66640-3e6ee0a0a8.zip/node_modules/tsx/dist/index-bd1ceb03.mjs:10:8007)
    at Xe (file:///Users/wataruian/sample/.yarn/cache/tsx-npm-4.7.0-86d7b66640-3e6ee0a0a8.zip/node_modules/tsx/dist/index-bd1ceb03.mjs:15:66)
    at Ie (file:///Users/wataruian/sample/.yarn/cache/tsx-npm-4.7.0-86d7b66640-3e6ee0a0a8.zip/node_modules/tsx/dist/index-bd1ceb03.mjs:15:297)
    at Z (file:///Users/wataruian/sample/.yarn/cache/tsx-npm-4.7.0-86d7b66640-3e6ee0a0a8.zip/node_modules/tsx/dist/esm/index.mjs:5:1900)
    at async nextLoad (node:internal/modules/esm/hooks:865:22)
    at async Hooks.load (node:internal/modules/esm/hooks:448:20)
    at async MessagePort.handleMessage (node:internal/modules/esm/worker:196:18) {
  idx: 143
}

Am I doing something wrong?

@wataruian
Copy link
Author

wataruian commented Jan 16, 2024

@mdonnalley

Update:

I was able to make tsx work via:

  1. Update dev.js with tsx instead of node --import tsx/esm (I can't make this shebang work):
#!/usr/bin/env tsx --no-warnings=ExperimentalWarning

async function main() {
  const { execute } = await import('@oclif/core');
  await execute({ development: true, dir: import.meta.url });
}

await main();
  1. Execute command via: yarn node --import tsx/esm bin/dev test-command

I am unsure if I did it clean enough or in best practice, but it works.

With this, I can compile the new code on the fly during dev mode which ts-node can't do in node:20

@mdonnalley
Copy link
Contributor

Closing this since users can switch to tsx or bun (oclif/core#966) or use a different node version

@mdonnalley mdonnalley unpinned this issue Mar 4, 2024
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

No branches or pull requests

2 participants