Sheet ⁨02⁩ · ⁨Blog⁩Surveyed ⁨2026⁩

Blog post image for Run TypeScript Without Compiling - We can run TypeScript without compiling it to JavaScript. This is useful for debugging and testing. In this post, I will show you how to do it.

Run TypeScript Without Compiling

Published: Updated: 02 Mins read02 Mins listen
Markdown for AI(opens in a new tab)

Introduction

In this post, I will show you how to run TypeScript without compiling it to JavaScript first. This is useful for debugging and testing.

Set up a TypeScript project

Step 1: create a directory

Create a directory for the project.

Terminal window
# Create a directory
mkdir run-typescript-without-compiling
# Change directory
cd run-typescript-without-compiling

Step 2: initialize a Node.js project

Initialize a Node.js project.

Terminal window
# Initialize a Node.js project
pnpm init

Step 3: install TypeScript and @types/node

Install TypeScript and @types/node.

Terminal window
# Install TypeScript and @types/node
pnpm add -D typescript @types/node

Step 4: initialize a TypeScript project

Initialize a TypeScript project.

Terminal window
# Initialize a TypeScript project
pnpm exec tsc --init

Step 5: create a TypeScript file

Create a TypeScript file.

Terminal window
# Create a src directory
mkdir src
# Create a TypeScript file
touch src/index.ts

Step 6: example code

Add the following code to the TypeScript file.

src/index.ts
const sum = (a: number, b: number): number => a + b;
const subtract = (a: number, b: number): number => a - b;
const multiply = (a: number, b: number): number => a * b;
const divide = (a: number, b: number): number => a / b;
console.log(sum(1, 2));
console.log(subtract(1, 2));
console.log(multiply(1, 2));
console.log(divide(1, 2));

Step 7: run the TypeScript file

Now we install tsx and run the TypeScript file directly. tsx supersedes the older ts-node for this job: it is faster, needs no configuration, and has a watch mode built in, so it replaces ts-node, esr and nodemon with a single dependency.

Terminal window
# Install tsx
pnpm add -D tsx

Add the following code to the package.json file.

package.json
{
...
"scripts": {
"playground": "tsx src/index.ts",
"playground:watch": "tsx watch src/index.ts",
"build": "tsc",
"build:watch": "tsc -w",
"build:debug": "tsc --sourceMap",
"build:debug:watch": "tsc -w --sourceMap",
"start": "node dist/index.js",
"start:watch": "node --watch dist/index.js"
},
...
}

Note

No extra watcher is needed. tsx watch restarts on changes to your TypeScript sources, and node --watch does the same for the compiled output in dist - it has been built into Node since 18.11, so there is nothing to install for either.

Run the TypeScript file.

Terminal window
# Run the TypeScript file
pnpm playground
# Output
3
-1
2
0.5

Conclusion

In this post, I showed how to run TypeScript without first converting it to JavaScript. For testing and troubleshooting, that is helpful.

References

Was this useful?

You might also enjoy

More posts on similar topics

TypeScript vs. JSDoc: Static Type Checking in JavaScript Compared

TypeScript vs. JSDoc: Static Type Checking in JavaScript Compared

Introduction Static type checking catches errors early and makes JavaScript code easier to maintain. Two popular tools for it are TypeScript and JSDoc. This article covers the pros and cons of eac

The ORM Dilemma: To Use or Not to Use

The ORM Dilemma: To Use or Not to Use

Introduction Some decisions shape a project more than others. One that keeps coming back is whether to use an Object-Relational Mapping (ORM) tool for database interactions. Should you skip an ORM

React Context API for State Management

React Context API for State Management

Introduction Managing application state well can make or break a React project. React offers several options for state management, and the Context API is one of the most flexible. But what exactly

Setting up JWT Authentication in TypeScript with Express, MongoDB, Babel, Prettier, ESLint, and Husky - Part 2

Setting up JWT Authentication in TypeScript with Express, MongoDB, Babel, Prettier, ESLint, and Husky - Part 2

Introduction Why do we even need an authentication mechanism in an application? In my opinion, it doesn't need to be explained. The phrases authentication and authorization have likely crossed you

6 related posts