You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!
The text was updated successfully, but these errors were encountered:
moranbw
changed the title
Are directories with index.js supported?
Are directories with index.ts supported?
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.
After looking into my configuration...I wasn't just "effectively setting moduleResolution: "node"...I actually explicitly had it set in my tsconfig.json 😂
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 inindex.ts
.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 withtsm
but no luck yet. I've been getting anERR_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!
The text was updated successfully, but these errors were encountered: