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

Are directories with index.ts supported? #39

Closed
moranbw opened this issue Aug 4, 2022 · 2 comments
Closed

Are directories with index.ts supported? #39

moranbw opened this issue Aug 4, 2022 · 2 comments

Comments

@moranbw
Copy link

moranbw commented Aug 4, 2022

I have a directory with a few files that I import into an index.ts file. I then import from the directory throughout my code (just feels clean). So something like this in index.ts.

import request1 from './request1';
import request2 from './request2';
import request3 from './request3';

export {request1, request2, request3};

Then used like this in other places in my project: import { request1, request2, request3 } from './requests';

To get this to work in ts-node I need to run like this: node --experimental-specifier-resolution=node --loader ts-node/esm src/main.ts

I believe --experimental-specifier-resolution=node is the key part here. I've been trying something similar with tsm but no luck yet. I've been getting an ERR_MODULE_NOT_FOUND error when trying to import from ./requests.

I'm probably just missing something simple, so my apologies if that's the case! Thanks in advance!

@moranbw moranbw changed the title Are directories with index.js supported? Are directories with index.ts supported? Aug 4, 2022
@lukeed
Copy link
Owner

lukeed commented Aug 4, 2022

No, not yet. I think this falls under maybe territory.

If you run tsm index.ts on its own in your example, you'll see an ERR_UNSUPPORTED_DIR_IMPORT error. This comes from Node directly because ESM itself doesn't allow directory imports as it doesn't include CommonJS's auto-index.js resolution logic.

It works in your ts-node configuration because you're effectively setting moduleResolution: "node", which is telling TS to specifically opt into Node's (classical) resolution behavior.

@moranbw
Copy link
Author

moranbw commented Aug 8, 2022

After looking into my configuration...I wasn't just "effectively setting moduleResolution: "node"...I actually explicitly had it set in my tsconfig.json 😂

Thank you for the clarification!

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