martins16321
martins16321

Reputation: 901

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

I am using ts-node but it is giving me this error:

$ ts-node index.ts

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/projects/node-hddds8/index.ts

I tried to remove "type": "module" from my package.json but in that case I get a different error:

$ ts-node index.ts

(node:45) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/projects/node-hddds8/index.ts:1
import chalk from 'chalk';
^^^^^^

SyntaxError: Cannot use import statement outside a module

Here is a reproduction link on StackBlitz: https://stackblitz.com/edit/node-hddds8?file=index.ts

My package.json looks like this:

{
  "name": "node-starter",
  "version": "0.0.0",
  "type": "module",
  "dependencies": {
    "chalk": "^5.0.1",
    "ts-node": "^10.8.1",
    "typescript": "^4.7.4"
  }
}

And my tsconfig.json looks like this:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "Node",
  }
}

And my index.ts looks like this:

import chalk from 'chalk';

console.log(chalk.blue('Hello world!'));

Upvotes: 30

Views: 50240

Answers (7)

Simon Bengtsson
Simon Bengtsson

Reputation: 8151

I ran into this error when using the new strip types flag in node 22.6 and using the wrong order argument order. It should be this:

node --experimental-strip-types main.ts

This does not work:

node main.ts --experimental-strip-types // TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

Upvotes: 0

Benny Code
Benny Code

Reputation: 54782

When using Node.js 18, ts-node and ESM, you can use:

  • ts-node index.ts
  • node --loader ts-node/esm index.ts
  • node --no-warnings=ExperimentalWarning --loader ts-node/esm index.ts (if you want to suppress experimental warnings)

When using Node.js 20, ts-node and ESM, you cannot use ts-node-esm anymore (it will show you ERR_UNKNOWN_FILE_EXTENSION). You have to use:

  • node --loader ts-node/esm src/main.ts

Unfortunately, this approach does not provide stack traces. Therefore, in the case of a compiler error, you will only see:

internalBinding('errors').triggerUncaughtException

You can work around this by using a combination of tsc and ts-node such as:

  • tsc --noEmit && node --loader ts-node/esm index.ts

Alternatively, you can use tsimp:

  • TSIMP_DIAG=error node --import=tsimp/import index.ts

I also made a tutorial showing each step:

Upvotes: 7

Adam
Adam

Reputation: 3288

After wrestling with this for many hours, I finally settled on tsx.

$ npm install --save-dev tsx

And then in your package.json:

{
  ...
  "scripts": {
    "dev": "tsx watch src/index.ts"
  }
}

Upvotes: 6

Martin Adámek
Martin Adámek

Reputation: 18359

While the solutions with ts-node-esm or adding esm: true to the ts-node options in tsconfig works with "older" node versions, with node >= 20, this fails again with ERR_UNKNOWN_FILE_EXTENSION.

A workaround is to use node --loader as described in this issue:

node --no-warnings=ExperimentalWarning --loader ts-node/esm file.ts

Upvotes: 17

RcoderNY
RcoderNY

Reputation: 1754

Since you have "type": "module" in your package.json you are using ESM modules, therefore you need to add this to your tsconfig.json:

{
    "compilerOptions": {
        "module": "ESNext" // or ES2015, ES2020
    },
    "ts-node": {
        // Tell ts-node CLI to install the --loader automatically
        "esm": true
    }
}

Upvotes: 28

Maxime Lattreche
Maxime Lattreche

Reputation: 91

Try to start your project with ts-node-esm index.ts

Upvotes: 6

user4500882
user4500882

Reputation: 391

Might be because your compilerOptions in typescript is set to {“module”: “esnext”}, so its getting interpreted as a module by ts-node. Try changing it to {“module”: “CommonJS”}

Upvotes: -1

Related Questions