ts-node: Error: unknown file extension .ts
Expected Behavior
Load typescript files
Actual Behavior
When I try to execute a typescript file I get the following error message: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension “.ts” for /home/$USERNAME/$PATH_TO_DEV_FOLEDER/index.ts at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15) at Loader.getFormat (internal/modules/esm/loader.js:113:42) at Loader.getModuleJob (internal/modules/esm/loader.js:243:31) at Loader.import (internal/modules/esm/loader.js:177:17)
Steps to reproduce the problem
launch ts-node index.ts
Minimal reproduction
Specifications
- ts-node version: v8.10.2 (installed global)
- node version: v14.3.0
- TypeScript version:v3.9.3 (installed global)
- tsconfig.json, if you’re using one:
{}
- Operating system and version: Windows 10 Version 2004
- If Windows, are you using WSL or WSL2?: Yes, Ubuntu 20.04 LTS in WSL2
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 97
- Comments: 77
Commits related to this issue
- mjs사용하려고 보니 ts-node 가 .ts를 못알아먹길레 https://github.com/TypeStrong/ts-node/issues/1062 — committed to JJhuk/docker-tag-bot by JJhuk 2 years ago
- fix: ts-node & lodash & esm ✅ https://github.com/TypeStrong/ts-node/issues/1062#issuecomment-1479292585 https://www.cnblogs.com/xgqfrms/p/17245062.html — committed to web-fullstack/patch-package-in-action by xgqfrms a year ago
- Apparently a dependency declaring "type": "module" impacts ts-node's ability to compile code https://github.com/TypeStrong/ts-node/issues/1062#issuecomment-983742878 — committed to gordonsyme/circleci-config-sdk-ts by gordonsyme 5 months ago
- fix(build): replace ts-node with tsx to resolve issues with modules (#1977) This pull request resolves #1926 and prevents issues like it from happening in the future ## Rationale for this PR This P... — committed to shadcn-ui/ui by Gravy59 5 months ago
- fix(build): replace ts-node with tsx to resolve issues with modules (#1977) This pull request resolves #1926 and prevents issues like it from happening in the future ## Rationale for this PR This P... — committed to frontbest0726/shadcn-ui by frontbest0726 5 months ago
Remove “type”: “module” from package.json
If remove module type, you cannot use imports in your .ts files
I must disagree. In Angular projects, there is no “type”: “module” in package.json and import synthax works fine. Try to set “module”: “es2020” in tsconfig.json
Closing as this is not a
ts-node
bug. If you want to use node’s native ESM support, which is currently experimental, then you can usets-node
’s experimental ESM loader hook. See #1007 for details.This stack overflow answer helped me: https://stackoverflow.com/questions/63445821/ts-node-execute-typescript-with-module-import-and-module-defined
Updating my tsconfig.json to migrate from
"module": "es6"
to"module": "commonjs"
allowed me to run my TypeScript file.Estou tendo o mesmo problema também
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for www\server\src\server.ts at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15) at Loader.getFormat (internal/modules/esm/loader.js:113:42) at Loader.getModuleJob (internal/modules/esm/loader.js:243:31) at Loader.import (internal/modules/esm/loader.js:177:17)
I’m having the same issue.
I remove type: module from package json and it work
My earlier suggestion should point you in the right direction.
On Sun, Jun 21, 2020, 9:44 AM AguinaldoAlberto notifications@github.com wrote:
ESM stuff? You might have better luck on our ESM thread. Or post a complete reproduction so someone else can debug.
I created a template repo which can be used by anyone who has trouble initializing
ts-node
.From #1007, use
node --loader ts-node/esm ./my-script.ts
if yourtsconfig.json
contains"module": "commonjs"
I’d like to report some success just for any lost soul who’s found there way here. I’ve discovered the below works fine for me–and mind you, this is “all modern” Typescript output as well as the Node
"type": "module"
use case, so YMMV but it certainly works with all of @sindresorhus’s newest ESM-only packages (find-up
,chalk
etc.). Seems to also work for standard-issue CommonJS packages.tsconfig.json
package.json
and the
ts-node
command:node --experimental-specifier-resolution=node --loader ts-node/esm src/main.ts
This also works with
tsc
andnode
, you just need to be sure to specify the same resolution mode. So, something along the lines oftsc && node --experimental-specifier-resolution=node dist/main.js
will work.Node version I’m using right now is
v14.17.3
–probably need to update but newer versions of NPM have issues with the corporate Artifactory 🥲Add this to the tsconfig.json:
and make sure to start the server with
ts-node
:It is crazy how hard it was for me to find this. This will allow you to use imports as well as use typescript.
I added “type”: “module” in package.json and did
npx tsx file.ts
instead of usingts-node file.ts
and it worked.ts-node --skip-project scripts/yourFile.ts
After hours lost this is the “solution”. Thanks.
set the value of module to
commonjs
in tsconfig.json, it works for me.I spent an hour banging my head against a wall this morning trying to figure this out - if you have a dependency in your project which uses
"type": "module"
in it, it can also cause this problem. In my case, I hadnode-fetch@3.1.0
. Downgrading tonode-fetch@2.5.12
fixed it for me.remove type: “module” and add in your tsconfig.json “compilerOptions”:{ “module”: “Commonjs”, “moduleResolution”: “node” }
Supported by ts-node 10.3.1
For me adding the
--esm
flag forts-node --esm index.ts
command did it.This is what worked for me ts-node: 10.2.1 typescript: 4.4.2 node: 16.8.0
in package.json I removed “target”: “module”
in tsconfig.json I added this
“ts-node”: { “compilerOptions”: { “module”: “commonjs” }
an in a separate “compilerOptions” “module”: “ESNEXT”
I was getting a different error which is “Cannot use import statement outside a module” as a suggested solution in stack overflow I added “target”:“module” in package.json which lead me to this error: unknown file extension .ts
Hope it helps you out. I also found this article https://www.tsmean.com/articles/learn-typescript/typescript-module-compiler-option/
We’ve enabled the discussion forum for questions like this one.
https://github.com/TypeStrong/ts-node/discussions
We’re trying to keep the issue tracker limited to actionable tasks, so contributors can focus on making fixes and improvements to
ts-node
.Since this is not a bug and nothing needs to change in
ts-node
, it’s not actionable, so it was closed. Discussions, on the other hand, are the perfect place to ask advice, share guides, and help with project configuration.Another good thing about the discussion forum: you don’t need to fill out an issue template.
No matter what I do, I can’t get beyond “Unknown file extension “.ts””. My project is in TypeScript, and I’m trying to import an npm package (double-metaphone) that only working as an ESM import. Since I’m trying run my code using:
node --experimental-specifier-resolution=node --loader ts-node/esm app.ts
…it seems utterly bizarre that I would get an error complaining about the “.ts” extension. What other than “.ts” would the ts-node loader expect?
I’ve tried various combinations and permutations of suggestion here and elsewhere. My latest setup, running under Node.js v16.13.1, looks like this:
Use
ts-node --esm
,ts-node-esm
, or set"esm": true
in tsconfig.jsonhttps://typestrong.org/ts-node/docs/imports/
This is what worked for me. Not even
ts-node-esm
worked.Let me just say that the entire ESM situation in Node.JS is a catastrophe. It created so much pain, incompatibility, lost hours. I don’t understand why some decisions were made that contributed to this pain. It’s probably not as bad as Python2 and Python3 but very close!
if your tsconfig.json contains “module”: “ESNext”. I have used the below script in my package.json. It worked.
What if you can not do that 😃? That’s not a solution.
I’m having the same issue. how to fix, thx
@a-h Thank you bro! you the man!
Following this ts-node documentation CommonJS vs native ECMAScript modules worked for me.
This is the solution for me, hope it helps someone I’m using Express 4.17.3, Node 14.17.0
My package.json file:
My tsconfig.json file:
@clinkadink thanks, that did it for me
What worked for me is
ts-node --esm <filename>
. NoesModuleInterop
orpackage.json
changesliterally what does angular have to do with node??? doesn’t matter if you’re using babel. the question is about using typescript with node so thank you for reading and understanding the question carefully in order to provide constructive and helpful guidance. Great job author!
I need
"type": "module"
in package.jsonnpx tsx ./file.ts
worked for meThis worked for me. Thanks, @retrotechie 🚀
I just
rm -rf dist
andtsc
&node dist/index.js
again, then it works.Solution 1:
Remove
"type": "module"
in package.jsonSolution 2:
package.json
tsconfig.json
I’m not sure if we had better solutions but these worked for me.
For me adding
--esm
did not work. I received the error:The file
/home/mike/Code/company/app/src/backend/functions.ts
does exist (and I shouldn’t need the .ts to load it in TS).However using
npm tsx
as others have mentioned did fix the problem and allowed my code to run perfectly.Real quick follow up to this - running with this makes my code ~8x slower than
ts-node
by itself with changed options or just usingtsc
andnode
, and that’s testing the same exact function 10 times in a loop to mitigate the effects of the typescript JIT compilation times. Hopefully this is useful to someone?Thanks! it’s useful.
If you suddenly started getting this issue, this might be the cause: https://github.com/TypeStrong/ts-node/issues/2094
It is actually the solution 😄
I got this same error (
Unknown file extension ".ts"
), trying to run a typescript script with ts-node 10.9.1 and Node 20.10. I couldn’t find a solution unfortunately (I tried"esModuleInterop": true
in tsconfig), but running the script through tsx worked, so I ended up migrating from ts-node to tsx.I find it really odd that .ts files are not recognised by default. Surely ts-node should know how to handle .ts files?
That’s not true. ESM and CJS (CommonJS) are two different things, changing
module
toCommonJS
will make tsc produce CommonJS code.To all the poor souls still battling this like me: I might have a solution with ESM, "type: “module” and ts-node for VSCode. It involves adding a --loader runtime arg to launch.json. I hope it helps!
(Windows, Node 18.2.0)
package.json:
tsconfig.json:
launch.json:
With this, I can use ESM import/export, run the VSCode debugger, catch breakpoints in my TS files, etc…
NOTE: I have to use import paths with .js filenames, which seems odd, but works. The actual included file is .ts, and the debugger steps into the .ts file. Example: import * as JOE from ‘./joelib.js’; works with my file “./joelib.ts”.
Specifying the loader via
NODE_OPTIONS
did work form me, even though I had"esm": true
in my tsconfig.json:nope, same problem with import. “Warning: To load an ES module, set “type”: “module” in the package.json or use the .mjs extension.”
for me worked the opposite ( updated node to the latest 21.x version ) the ts-node config part in tsconfig.json
same error in 2023
anyone code a tsx launch file for vscode that allows debugging?
Thank you😘
ts-node error ❌
ESM solution ✅
refs
https://typestrong.org/ts-node/docs/imports/
Receiving unknown file extension “.ts” error after upgrading wdio to 8 and Appium >16.
Detailed description here.
This work!
Very interesting but why
npm
doesn’t work?I am trying to use Typescript from Webpack but latest Angular updated their webpack plugins to ESM. 😄 Eh, the ESM is such a disaster that one can only laugh. So many hours needlessly wasted. Unfortunately none of the solutions in this thread help.
For anyone who is running into this issue on CI or on Node.js 16.12.0, it seems like this latest version has made some changes to the hooks API 😱
https://github.com/nodejs/node/releases/tag/v16.12.0
Also mentioned here:
https://github.com/TypeStrong/ts-node/issues/1007#issuecomment-948484016
Yup that works!!