#TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

86 messages · Page 1 of 1 (latest)

mossy thunder
#

I'm getting the TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" error when i run my project.

I believe i configured my tsconfig.json correctly and i've been searching for hours but can't find a solution that works

tsconfig.json

{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig to read more about this file */

    "target": "ES2022",
    "module": "ES2022",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "moduleResolution": "NodeNext" ,
    "noEmit": true,
    "allowImportingTsExtensions": true,
  },
  "ts-node": {
    "esm": true
  }
}

package.json

{
  "main": "src/index.ts",
  "type": "module",
  "scripts": {
    "dev": "nodemon src/index.ts"
  },
  "dependencies": {
    ...
  },
  "devDependencies": {
    "@types/node": "^20.3.3",
    "ts-node": "^10.9.1"
  },
  "overrides": {
    "semver": "7.5.2"
  }
}

run command npx ts-node src/index.ts

neat parrot
#

node can't run .ts files and will error with that message, both your main entry and your dev script are using node (unless you have nodemon configured). are you sure you're using ts-node when you get that error?

mossy thunder
neat parrot
#

how did you run your code?

mossy thunder
neat parrot
#

please copy the command directly from your terminal rather than retyping it

#

the command you showed will error with something different

mossy thunder
mossy thunder
neat parrot
#

you typed index.s there

mossy thunder
#

ah my bad but it wasn't the command i ran in the terminal anyway

#

i just retyped it and make a mistake

neat parrot
#

ah you're probably importing .ts files, forgot that was a thing

#

you need to be importing .js in your code

mossy thunder
neat parrot
#

you are

mossy thunder
#

like it isn't creating the build folder

neat parrot
#

ts-node does the transpilation, hidden away

mossy thunder
#

ah

neat parrot
mossy thunder
#

didn't know

neat parrot
#

node just cannot run ts

#

ts-node is a layer between ts and node, it's not its own runtime

mossy thunder
#

so when i run the command do i type index.ts or index.js?

neat parrot
#

runtimes like deno that can run typescript natively could truly not have the build step, but that isn't the case for node

neat parrot
#

but in the source code, you need to be importing using .js rather than .ts

mossy thunder
#

index.ts is the entry point of the project

neat parrot
#

since that's the job of the runtime, so node handles that
ts-node doesn't rewrite the imports from .ts to .js

true pier
#

but realistically, not without patching v8

mossy thunder
#

An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.

true pier
#

import from .js instead

mossy thunder
neat parrot
#
- import { stuff } from "./otherfile.ts";
+ import { stuff } from "./otherfile.js";
mossy thunder
#

i did that

neat parrot
mossy thunder
#

setting it to false solved it

#

but it still says unknown file type .ts

neat parrot
#

check the lines it points to

#

you might not have changed all the imports

mossy thunder
#

there are no imports using .ts

mossy thunder
#

which has no .ts imports

neat parrot
#

check the line it points to

#

also make sure you've saved your files

mossy thunder
#

might it be that there is a dynamic import that reads files from the directory

mossy thunder
neat parrot
mossy thunder
#

wdym

neat parrot
#

errors give you the line that they occured on

mossy thunder
#

yea

#

it's the path to the src/index.ts

neat parrot
#

that's the file

#

the error tells you what line in the file errored

mossy thunder
#
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for C:\Users\user\Desktop\Development\folder\Bot v2\src\index.ts
    at new NodeError (node:internal/errors:405:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:99:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:142:36)
    at defaultLoad (node:internal/modules/esm/load:86:20)
    at nextLoad (node:internal/modules/esm/hooks:726:28)
    at load (C:\Users\user\Desktop\Development\folder\Bot v2\node_modules\ts-node\dist\child\child-loader.js:19:122)
    at nextLoad (node:internal/modules/esm/hooks:726:28)
    at Hooks.load (node:internal/modules/esm/hooks:370:26)
    at MessagePort.handleMessage (node:internal/modules/esm/worker:168:24)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:762:20) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
mossy thunder
true pier
#

wait are you still trying to run a ts file

neat parrot
#

oh damn yeah

mossy thunder
neat parrot
#

and you're running this via...?

mossy thunder
#

npx ts-node src/index.ts

neat parrot
#

odd, it should resolve that...

mossy thunder
#

yea i have been stuck on this for hours

true pier
#

nods version might be too new possibly

mossy thunder
#

should i try with 18.16.1 which is lts

true pier
#

yeah

mossy thunder
#

ok give me a second

mossy thunder
#

now it doesn't give the unknown extension error anymore

true pier
#

itll definitely get fixed eventually

#

no clue when though

mossy thunder
#

just that now something don't work anymore

#

__dirname is not defined

true pier
#

wait wtf

#

npm i @types/node i guess

mossy thunder
#

it appears it's not defined in es6 syntax

#

it only works with cjs

neat parrot
#

yes

true pier
#

all ts is es6 syntax