Replies: 11 comments 3 replies
-
That section has a detailed breakdown of how file extensions work. I recommend reading it thoroughly. Remember that this is the way that TypeScript has chosen to do things, so ts-node follows along with those rules. Even if you are not using Worth noting also that the
Can you copy-paste the complete terminal output that you see when getting this error? I suspect that there is additional context, perhaps some lines of output preceding or following that error. It will help to know what is giving you this error. I suspect node is giving it to you, but could also be typescript. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
My guess is you're still stuck putting a .ts extension in your import statements when you should put a .js extension instead. Try it even if it seems wrong; it'll work. |
Beta Was this translation helpful? Give feedback.
-
Sadly this works. Thanks for the help even though this isn't a ts-node problem but a typescript problem. It feels really wrong to put a ".js" extension behind my ts imports... I'm also bummed out that my auto imports don't auto add the extension. Hopefully I'll find some kind of import resolver that just adds the extension behind the scenes... For now switching to |
Beta Was this translation helpful? Give feedback.
-
Auto-imports can be configured to add the extension. There's a vscode setting for it; they also automatically mimic style of the other imports in the file. |
Beta Was this translation helpful? Give feedback.
-
Alright I'll have a look. |
Beta Was this translation helpful? Give feedback.
-
TLDR version: where using type: module, put .js even when you are importing .ts. This will work even though it seems counter intuitive :) |
Beta Was this translation helpful? Give feedback.
-
Thanks to this thread I managed to run and debug my TS even without node --watch --loader ts-node/esm ./src/index.ts here is an example of my |
Beta Was this translation helpful? Give feedback.
-
you can also try adding this flag |
Beta Was this translation helpful? Give feedback.
-
or adding this to "ts-node": {
"esm": true,
"experimentalSpecifierResolution": "node",
} |
Beta Was this translation helpful? Give feedback.
-
FWIW, when code reaches high McGabe's humans simply can't follow anymore, and we are way beyond this point with the 3 different configuration points ( It's going to be a rough time transitioning to ESM. |
Beta Was this translation helpful? Give feedback.
-
Search Terms
ts-node, ERR_MODULE_NOT_FOUND
Expected Behavior
run my typescript es module files.
Actual Behavior
I have a project that looks like this
I have a "start" script in my package.json that I run via
pnpm start
that executests-node ./src/main.ts
. (I have also tried this with npm and it also didn't work)When I run this I get the error "Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.". So I do as I'm told and I add it to my package.json and run it again.
Now I get this error: "TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for [path-to-project]/src/main.ts"
I manage to resolve this error by adding "--esm" to my start script like this:
ts-node --esm ./src/main.ts
. If I run my start script I get a new error: "Cannot find module '[path-to-project]/src/module' imported from [path-to-project]/src/main.ts"I can surpass this error by doing this:
Now my code runs but I have some issues with this
How can I make my code work with ts-node by removing the ".ts" extension in the import statement?
Minimal reproduction
Specifications
Beta Was this translation helpful? Give feedback.
All reactions