Skip to content

Wrong root(cwd) determination for esm modules #1881

Closed

Description

@ThePlenkov

Hi! During debug I have noticed something interesting. To use top-level await I've changed package to module and activated esm for ts-node. However right after this import for json files stopped working.
Here is what I have in IDE
image

And here is the base path ts-node is trying to resolve workspace.json against to.

image

I think you should take root path from tsconfig to resolve imports, same as typescript does. Otherwise is different logic

For now I'd prefer not to use ts-node with esm: true.

Thanks!

Activity

  1. ThePlenkov commented on Sep 11, 2022

    @ThePlenkov
    Author

    it's important - I run the command from the root npx my-command where my-command is a link to a workspace ts file with ts-node shebang

  2. ThePlenkov commented on Sep 11, 2022

    @ThePlenkov
    Author

    So current workaround instead of import to use dynamic import:

    // import { projects } from 'workspace.json' assert { type: 'json' };
    
    const projects = (await import(path.resolve(cwd(), 'workspace.json'), {
      assert: { type: 'json' },
    })) as { default: { projects: Record<string, string> } };
    
  3. locked and limited conversation to collaborators on Sep 11, 2022
  4. converted this issue into a discussion #1884 on Sep 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions