-
-
Notifications
You must be signed in to change notification settings - Fork 538
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
ERR_MODULE_NOT_FOUND using es modules #1777
Comments
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. |
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. |
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 |
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. |
Alright I'll have a look. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
The text was updated successfully, but these errors were encountered: