How to run a TypeScript file from the Command line

avatar
Borislav Hadzhiev

Last updated: Feb 27, 2024
3 min

banner

# Run a TypeScript file from the Command line

Use the ts-node package to run a TypeScript file from the command line, e.g. npx ts-node myDirectory/myFile.ts.

The ts-node command will transpile the TypeScript file to JavaScript and will run the code in a single step.

Here is the TypeScript file we will run from the command line.

src/index.ts
function sum(a: number, b: number) { console.log(`The result is: ${a + b}`); return a + b; } sum(20, 30); // 👉️ 50
The code for this article is available on GitHub

The file is located at src/index.ts and I have opened my terminal in the directory that contains the src directory.

To run the TypeScript file, we have to use the ts-node package.

# Specify the correct path to the file

Open your terminal in a directory from which you want to run the file and specify the correct path to the file.

shell
npx ts-node src/index.ts
Make sure to specify the correct path to the file you want to run. The path to the file is also determined by where you open your terminal.

run typescript file command line

# Installing typescript

If you haven't installed TypeScript on your machine, you can do so by running the following command.

shell
npm install -g typescript # Depending on your configuration, you may also need the typings npm install -D tslib @types/node

install typescript globally

The code for this article is available on GitHub

If you get an error about not having sufficient permissions when installing TypeScript globally, prefix the command with sudo (macOS or Linux), or open CMD as an administrator (Windows).

shell
sudo npm install -g typescript

# Running typescript files from the command line

The npx ts-node myFile.ts command transpiles the TypeScript code to JavaScript and runs the code with Node.js.

shell
npx ts-node src/index.ts

issue npx ts node command

You could manually do this by running the tsc (TypeScript compiler) and node commands yourself.

shell
tsc src/index.ts node src/index.js

manually issuing tsc command

The tsc myFile.ts command will generate a .js file with the same name in the same directory.

Here is the src/index.js file that was generated.

src/index.js
function sum(a, b) { console.log('The result is: '.concat(a + b)); return a + b; } sum(20, 30); // 👉️ 50

Now we can run the file with node.

shell
node src/index.js

This is exactly what ts-node does under the hood, but it does it without generating a JavaScript file.

If you have a TypeScript project with a tsconfig.json file, you can open your terminal in the root directory (the one with tsconfig.json) and start tsc in watch mode.

shell
tsc --watch -p . # or with npx npx tsc --watch -p .
The code for this article is available on GitHub

Where the generated JavaScript files will be placed depends on the outDir option in your tsconfig.json file.

For example, if you have outDir set to a directory named build, the TypeScript compiler will watch for changes in your project, transpile your .ts files to .js files and add the .js files to your build directory.

You can run any .js file with node myFile.js and any TypeScript file with npx ts-node myFile.ts.

Notice that we use npx when running the ts-node command.

npx basically checks if a package (ts-node in this case) is installed locally (in package.json) or installed globally and runs it.

If the package is not installed, npx downloads and runs the package.

If you get the error tsc command not found, click on the link and follow the instructions.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.