lukeed / tsm

TypeScript Module Loader
MIT License
1.19k stars 19 forks source link

Are directories with index.ts supported? #39

Closed moranbw closed 2 years ago

moranbw commented 2 years ago

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!

lukeed commented 2 years ago

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 commented 2 years ago

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!