The fastest way to run TypeScript is to install Node.js, then use `npx ts-node` to execute your file directly without compiling first

TypeScript is a layer on top of JavaScript that adds type checking. Your computer cannot run TypeScript directly — it has to become JavaScript first. You have two main paths: compile TypeScript to JavaScript and then run it, or use a tool that does both steps at once. The quickest route for testing and development is ts-node, which handles the conversion invisibly and runs the result immediately.

If you want to deploy code to production or share it with others, you will compile TypeScript to JavaScript files first, then run those JavaScript files. This separation gives you more control and works on any machine with Node.js, even if TypeScript is not installed.

Key Takeaways

  • Install Node.js from nodejs.org, which includes npm (the package manager you need to get TypeScript tools).
  • The fastest way to run a TypeScript file during development is `npx ts-node yourfile.ts`, which requires no setup beyond Node.js.
  • To run TypeScript in production, compile it to JavaScript first with `tsc yourfile.ts`, then run the resulting `.js` file with `node yourfile.js`.
  • TypeScript needs a configuration file called `tsconfig.json` if you are compiling multiple files or want to control how strict the type checking is.

Install Node.js first

TypeScript runs on top of Node.js, so you need Node.js installed before anything else. Go to nodejs.org, download the LTS (Long Term Support) version for your operating system, and run the installer. On Windows, this is an `.msi` file. On Mac, it is a `.pkg` file. On Linux, follow the instructions for your distribution.

When the installation finishes, open a terminal or command prompt and type `node --version`. You should see a version number like `v20.10.0`. This confirms Node.js is installed and ready. Node.js also installs npm, the package manager, which you will use to get TypeScript tools.

Run a TypeScript file with ts-node (development)

The simplest way to run TypeScript while you are writing and testing is to use a tool called ts-node. It compiles your TypeScript to JavaScript in memory and runs it immediately, so you see the output right away without creating extra files.

Open a terminal in the folder where your TypeScript file lives. If your file is called `hello.ts`, type this command:

npx ts-node hello.ts

The first time you run this, npm will download ts-node automatically (the `npx` command does this). Then ts-node will run your file. If your file has any TypeScript errors, ts-node will print them to the terminal and stop. Fix the errors and run the command again.

You do not need to install TypeScript itself or create a configuration file to use ts-node. It works with the default settings and is ideal for scripts, learning, and quick testing.

Compile TypeScript to JavaScript (production)

When you are ready to deploy code or share it with others, you should compile your TypeScript to JavaScript files. This creates actual `.js` files that any machine with Node.js can run, even if TypeScript is not installed.

First, install TypeScript globally so you have the compiler available everywhere:

npm install -g typescript

Then, in the folder with your TypeScript file, run the compiler:

tsc hello.ts

The `tsc` command (TypeScript compiler) reads `hello.ts` and creates a new file called `hello.js` in the same folder. The `.js` file contains the compiled JavaScript. Now run it with Node:

node hello.js

You will see the same output as if you had used ts-node, but now you have a JavaScript file you can move, share, or deploy anywhere.

Set up tsconfig.json for multiple files

If you have more than one TypeScript file, or if you want to control how strict the type checking is, create a file called tsconfig.json in your project folder. This file tells the TypeScript compiler how to behave.

A basic tsconfig.json looks like this:

{ "compilerOptions": { "target": "ES2020", "module": "commonjs", "strict": true, "outDir": "./dist" } }

This tells TypeScript to compile to JavaScript that works in modern browsers and Node.js, use CommonJS modules (the standard for Node.js), enforce strict type checking, and put all compiled files in a folder called `dist`. Once you have a tsconfig.json, you can compile all your TypeScript files at once by just typing `tsc` with no filename.

The `outDir` option is useful because it keeps your source TypeScript files separate from the compiled JavaScript. You then run files from the `dist` folder: `node dist/hello.js`.

Troubleshooting common errors

If you see "command not found: ts-node", Node.js is not installed or not in your system path. Restart your terminal after installing Node.js, or check that `node --version` works first.

If you see "error TS2307: Cannot find module", your TypeScript file is trying to import something that does not exist. Check the import path and make sure the file or package is actually installed. If you are importing from npm packages, run `npm install packagename` first.

If you see "error TS2322: Type 'string' is not assignable to type 'number'", you have a type mismatch. TypeScript caught a mistake where you are using the wrong type of value. Read the error message — it tells you which line and what the problem is. Fix the code and run the command again.

If ts-node runs but your code does not produce output, check that your code actually prints something. A TypeScript file that does calculations but never calls `console.log()` will run silently. Add `console.log()` to see results.

Frequently Asked Questions

Do I need to install TypeScript if I only use ts-node?

No. The `npx ts-node` command downloads and runs ts-node without installing anything permanently. You only need to install TypeScript globally if you want to use the `tsc` compiler command directly, or if you prefer to have it available offline.

What is the difference between ts-node and compiling with tsc?

ts-node compiles and runs in one step, which is fast for development and testing. The `tsc` compiler creates actual JavaScript files, which is better for production because the compiled code runs faster and does not require TypeScript to be installed on the machine running it.

Can I run TypeScript in a browser?

Not directly. Browsers run JavaScript, not TypeScript. You must compile TypeScript to JavaScript first. For browser projects, tools like Webpack or Vite handle this compilation automatically as you develop, so you can write TypeScript and see it run in the browser without manual steps.

What does "strict": true in tsconfig.json do?

It turns on all of TypeScript's strictest type-checking rules. This catches more potential bugs but also requires you to be more explicit about types. If you are learning TypeScript, starting with `"strict": false` is easier, then turning it on once you understand the basics.

Why does my compiled JavaScript file look so different from my TypeScript?

TypeScript adds syntax that JavaScript does not understand — like type annotations and interfaces. The compiler strips all of that out and converts modern JavaScript features to older syntax if needed (based on your `target` setting in tsconfig.json). The result is plain JavaScript that any Node.js version can run.