0

I'm trying to create a barebones repo that sets up a project using ts-node and ESM. This question has been asked, but there are several out of date answers- and even recent answers seem to have issues such as here

I've created a src/index.ts file that I am trying to run:

src/index.ts

console.log('hi');

Here is my package.json:

{
  "name": "coolProjName",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "compile": "tsc",
    "dev": "ts-node --esm src/index.ts"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^20.4.2",
    "ts-node": "^10.9.1",
    "typescript": "^5.1.6"
  }
}

And my tsconfigs:

tsconfig.node20.json

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Node 20",
  "_version": "20.1.0",

  "compilerOptions": {
    "lib": ["ES2022"],
    "module": "Node16",
    "target": "es2022",

    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node16"
  }
}

tsconfig.json

{
    "extends": "./tsconfig.node20.json",
    "compilerOptions": {
      "module": "esnext",
      "outDir": "./dist",
    },
    "include": ["./src/**/*"]
}

I've followed every recommendation I can find, but npm run dev results in:

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

for my index.ts file. How can I resolve this?

Node version - 20.4.0

Edon
  • 1,116
  • 2
  • 22
  • 47

1 Answers1

0

Remove "type": "module" from package.json

If you don't want to remove "type": "module" (for example if you're using import statements in your .ts which allows the inference of types from modules), then you can use the following option in tsconfig.json:

{
 "compilerOptions": {
   "esModuleInterop": true,
 }
}

Source

chill389cc
  • 365
  • 1
  • 10