Getting `TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"` in `ts-node` when type is set to module

222 views Asked by At

I configured absolute path with in my express project and then changed the type to module since I am using import but this is giving me error TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

Here is my tsconfig.json

{
  "compilerOptions": {
      "module": "ESNext",
      "moduleResolution": "bundler",
      "esModuleInterop": true,
      "forceConsistentCasingInFileNames": true,
      "strict": true,
      "skipLibCheck": true,
      "outDir": "./dist",
      "sourceMap": true,
      "incremental": true,
      "baseUrl": ".",
      "paths": {
          "@routes/*": ["./src/routes/*"],
      },
  },
  "include": ["src/**/*"],
  "ts-node": {
    "swc": true,
    "esm": true,
    "experimentalSpecifierResolution": "node"
  },
  "exclude": ["node_modules"]
}

here is my package.json

{
...
  "version": "1.0.0",
  "description": "",
  "main": "./src/index.ts",
  "type": "module",
  "scripts": {
    "build": "npx tsc",
    "start": "node ./dist/index.js",
    "dev": "nodemon --watch './src/**/*.ts' --exec 'ts-node --swc --esm' ./src/index.ts",
    "prettier": "prettier --write .",
    "prepare": "husky install"
  },
  "lint-staged": {
    "**/*": "prettier --write --ignore-unknown"
  },
...
}

I tried using --esm flag to ts-node but there is still the same issue. what I want os to make the absolute import work but if I remorve ts-node and use concurrently \"npx tsc --watch\" \"nodemon -q ./dist/index.js\" then ansolute imports are not working. I am kind of stuck in a loop.

0

There are 0 answers