Error: Unknown file extension ".ts" when using Express and TypeScript with Nodemon

521 views Asked by At

I'm encountering an issue when trying to start a project using Express and TypeScript with Nodemon. When I run Nodemon, I receive the following error:

[nodemon] 3.0.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): src\**\*
[nodemon] watching extensions: ts,js
[nodemon] starting `ts-node ./src/index.ts`
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for C:\Users\jdoro\Programacion\GitHub\my-react-projects\eventify-io\backend\src\index.ts
    at new NodeError (node:internal/errors:393:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:75:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:114:38)
    at defaultLoad (node:internal/modules/esm/load:81:20)
    at nextLoad (node:internal/modules/esm/loader:161:28)
    at ESMLoader.load (node:internal/modules/esm/loader:594:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:446:22)
    at new ModuleJob (node:internal/modules/esm/module_job:64:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:469:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:423:34) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
[nodemon] app crashed - waiting for file changes before starting...

Here is my Nodemon configuration:

{
  "ignore": [
    ".git",
    "node_modules"
  ],
  "watch": [
    "src"
  ],
  "exec": "ts-node ./src/index.ts",
  "ext": ".ts,.js"
}

It seems like Nodemon is having trouble recognizing the .ts file extension. Has anyone encountered this issue before, and is there a solution to get Nodemon to work with TypeScript files in this setup?

Thank you for your help!

I have tried configuring Nodemon to watch TypeScript (.ts) files

I was expecting Nodemon to successfully watch and restart when changes are made to TypeScript files (.ts) within the "src" directory and execute the TypeScript code in ./src/index.ts using ts-node.

However, it seems that Nodemon is throwing an error with the message: "Unknown file extension ".ts" for C:\Users\jdoro\Programacion\GitHub\my-react-projects\eventify-io\backend\src\index.ts." This suggests that Nodemon is not recognizing TypeScript files (.ts) as valid extensions for monitoring and restarting.

I'm seeking guidance on how to configure Nodemon to work with TypeScript files in this Express and TypeScript project. Any help or insights would be greatly appreciated.

(Edit)

I've also tried running the following command:

"dev": "nodemon --exec node --loader ts-node/esm src/index.ts",

But it resulted in the following error:

    > nodemon --exec node --loader ts-node/esm src/index.ts

[nodemon] 3.0.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts,json
[nodemon] starting `node --loader ts-node/esm src/index.ts`
(node:17548) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
ReferenceError: exports is not defined in ES module scope
    at file:///C:/Users/jdoro/Programacion/GitHub/my-react-projects/eventify-io/backend/src/index.ts:5:23
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
[nodemon] app crashed - waiting for file changes before starting...

It seems like there may be an issue with ESM (ECMAScript Modules) and Nodemon's configuration. Has anyone encountered this particular error before, and is there a solution to make Nodemon work with TypeScript files in this setup?

0

There are 0 answers