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

Wrong base path determination when ts-node/esm loader is used #1989

Open
ThePlenkov opened this issue Apr 11, 2023 · 4 comments
Open

Wrong base path determination when ts-node/esm loader is used #1989

ThePlenkov opened this issue Apr 11, 2023 · 4 comments

Comments

@ThePlenkov
Copy link

ThePlenkov commented Apr 11, 2023

Current behavior

Let's say I have a subpackage in my workspace which I want to use a module .

So i have a folder like tools/codegen with file like index.mts with a content like

#!/usr/bin/env -S node --loader ts-node/esm
const t = await true;

So without tsconfig.json tsc will not work. So let's add tools/codegen/tsconfig.json

{
    "extends": "@tsconfig/recommended/tsconfig.json",
    "compilerOptions": {
        "module": "ESNext",
        "target": "ES2017",
        "moduleResolution": "node16",
        "esModuleInterop": false
    },
    "ts-node": {
        "esm": true
    }
}

So with this config IDE already doesn't give error. tsc command also works fine.

In my tools/codegen/package.json i have "bin":"index.mts"

So after install I 'm able to run npx codegen from tools/codegen

However if I run it from the root of the monorepo it seems that ts-node is not smart enough to determine what is the base folder for executed file. So command npx codegen from the root level triggers top-level await TypeScript error which occurs without tsconfig.json

To be able to run npx codegen from root level i use "workspaces" : "tools/*" in my root package.json

Expected behavior

I expect that ts-node should understand from where it is called. Indeed node is called with some path and that path can be used as a base path for config determination.

It should not matter from where I call my npx codegen command - it should work similarly becase config should be linked to the executed file and not to the folder from where I run.

In my case I don't have tsconfig.json - but in more complicated case I might have one but with different set up.

Example

I've prepared a sample project showing this behavior https://github.com/ThePlenkov/ts-config-top-level-await-issue

Thanks!

@crobinson42
Copy link

I switched to tsx and everything works 👌

@ThePlenkov
Copy link
Author

Hi @crobinson42 could you please elaborate more what did you exactly do? Just renamed files? Thanks!

@ThePlenkov
Copy link
Author

Oh ok! So tsx is something new for me. I guess you meant https://github.com/esbuild-kit/tsx. I just updated my sample https://github.com/ThePlenkov/ts-config-top-level-await-issue and indeed tsx just works smoothly. Many thanks for this tip! However I'll keep the issue open here as it's ts-node realted

@randre70
Copy link

tsx is fine as long as you won't use decorators which esbuild doesn't support. esbuild has not type support.

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

3 participants