---
title: "Run TypeScript Without Compiling"
description: "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."
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/blog/post/run-typescript-without-compiling
---

# Run TypeScript Without Compiling

## Introduction

In this post, I will show you how to 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.

## Setup a TypeScript Project

### Step 1: Create a Directory

Create a directory for the project.

```shell
# 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.

```shell
# Initialize a Node.js project
yarn init -y
```

### Step 3: Install TypeScript and `@types/node`

Install TypeScript and `@types/node`.

```shell
# Install TypeScript and @types/node
yarn add -D typescript @types/node
```

### Step 4: Initialize a TypeScript Project

Initialize a TypeScript project.

```shell
# Initialize a TypeScript project
yarn tsc --init
```

### Step 5: Create a TypeScript File

Create a TypeScript file.

```shell
# 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.

```ts title=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 will install `ts-node` and run the TypeScript file.

```shell
# Install ts-node
yarn add -D ts-node
```

Add the following code to the `package.json` file.

```json title=package.json
{
  ...
  "scripts": {
    "playground": "ts-node src/index.ts",
    "playground:watch": "nodemon -e ts -w . -x esr 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": "nodemon -e js -w dist -x esr dist/index.js"
  },
  ...
}
```

<br />

  `nodemon` is a utility that will monitor for any changes in your source and
  automatically restart your server. You can install it using `yarn global add
  nodemon` or `npm install -g nodemon`.

Run the TypeScript file.

```shell
# Run the TypeScript file
yarn playground

# Output
3
-1
2
0.5
```

## Conclusion

I demonstrated how to run TypeScript in this article without converting it to JavaScript. For testing and troubleshooting, this is helpful. I demonstrate how to accomplish that in this post.

## References

- [TypeScript Official Website](https://www.typescriptlang.org/)
- [TypeScript Documentation](https://www.typescriptlang.org/docs/)
- [ts-node on npm](https://www.npmjs.com/package/ts-node)
- [ts-node GitHub Repository (TypeStrong/ts-node)](https://github.com/TypeStrong/ts-node)
- [nodemon on npm](https://www.npmjs.com/package/nodemon)
- [nodemon Official Website](https://nodemon.io/)
- [Node.js Official Website](https://nodejs.org/)
- [Node.js Documentation](https://nodejs.org/en/docs/)
- [Yarn Package Manager](https://yarnpkg.com/)
- [TypeScript `tsconfig.json` Reference](https://www.typescriptlang.org/tsconfig)
- [npm `package.json` Guide](https://docs.npmjs.com/cli/v7/configuring-npm/package-json)
- [`@types/node` package on npm](https://www.npmjs.com/package/@types/node) (for Node.js type definitions)
- [TypeScript Playground](https://www.typescriptlang.org/play) (for quick experiments)
