TypeScript .ts file not working? Fixing the "Unknown file extension" error

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: "Your .ts file won't open? You're not alone"

\n\n

Imagine: you're writing awesome TypeScript code, save the file with the .ts extension, run the command node myfile.ts... and get something like this in response: "Unknown file extension ".ts". Sound familiar? If you've looked on Stack Overflow, you know this question has gathered thousands of views and hundreds of comments. The problem is old, but still relevant.

\n\n

In this article, we won't just copy an answer from a forum. We'll break down why this happens, how to fix it, and what to do to never encounter this error again. You'll learn how to properly configure a TypeScript project, use ts-node and the tsconfig.json configuration. Let's go!

\n\n

Why does the "Unknown file extension ".ts"" error occur?

\n\n

Let's get straight to the point. Node.js does not understand .ts files by default. It only knows .js, .json, .mjs, and .cjs. When you write node myfile.ts, Node.js honestly tries to execute the TypeScript code as regular JavaScript, but encounters syntax it doesn't recognize (for example, types, interfaces, generics).

\n\n

The result is an error. But there's another scenario: you're using ts-node or tsx, but still see the same message. Why? Because in modern versions of Node.js (starting from version 16), strict extension checking was added in package.json, especially when "type": "module" is set.

\n\n

Main causes:

\n
    \n
  • Direct execution of node file.ts — Node.js cannot compile TypeScript on the fly.
  • \n
  • Missing ts-node or tsx — no tool for transpilation.
  • \n
  • Incorrect tsconfig.json configuration — for example, module or target not specified.
  • \n
  • Issues with package.json — if "type": "module" is set, ts-node may require explicit extension specification.
  • \n
\n\n

Solution 1: Using ts-node (classic method)

\n\n

The simplest way to run TypeScript without prior compilation is to use ts-node. This package compiles .ts files in memory and passes the result to Node.js.

\n\n

Installation and execution

\n
npm install -g ts-node\nts-node myfile.ts
\n\n

If you're using a local project, it's better to install it as a dev dependency:

\n
npm install --save-dev ts-node\nnpx ts-node myfile.ts
\n\n

Example of a simple hello.ts file:

\n
const greet = (name: string): string => {\n    return `Hello, ${name}!`;\n};\n\nconsole.log(greet("World"));
\n\n

Run it: npx ts-node hello.ts — and you'll see "Hello, World!" in the console. It works!

\n\n

But there's a catch: "type": "module"

\n

If your package.json has "type": "module", ts-node may refuse to work. In this case, you can either temporarily remove this line or use the --esm flag:

\n
npx ts-node --esm myfile.ts
\n\n

Solution 2: Using tsx (modern tool)

\n\n

tsx is a faster alternative to ts-node, based on esbuild. It works with ESM out of the box and doesn't require additional flags.

\n\n

Installation:

\n
npm install --save-dev tsx
\n\n

Execution:

\n
npx tsx myfile.ts
\n\n

Example using modern TypeScript features:

\n
// types.ts\nexport interface User {\n    id: number;\n    name: string;\n    email: string;\n}\n\nconst user                

Blogs

Book Recommendations