JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

How to run TypeScript files from the command line

Author:JIYIK Last Updated:2025/04/06 Views:

Run TypeScript files from the command line using the ts-node package, for example npx ts-node myDirectory/myFile.ts. ts-nodeThe command will convert the TypeScript file to JavaScript and run the code in one step.

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

function sum(a: number, b: number) {
  console.log(`The result is: ${a + b}`);
  return a + b;
}

sum(20, 30); // 👉️ 50

The file is located at src/index.ts, I opened my terminal in the directory containing the src directory.

To run TypeScript files, we have to use ts-nodethe package.

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

$ 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 also depends on where we open the terminal.

Run TypeScript files from the command line

If we don’t already have TypeScript installed on our computer, we can install it by running the following command:

$ npm install -g typescript

# Depending on configuration, you may also need these
$ npm install -D tslib @types/node

If you get an insufficient permissions error when installing TypeScript globally, prepend the command sudo.

$ sudo npm install -g typescript

npx ts-node myFile.tsThe command converts the TypeScript code to JavaScript and runs the code using Node.js.

We can do this manually by running the tsc (TypeScript compiler) and node commands ourselves:

$ tsc src/index.ts

$ node src/index.js

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

Here is the generated src/index.js file.

function sum(a, b) {
  console.log('The result is: '.concat(a + b));
  return a + b;
}

sum(20, 30); // 👉️ 50

Now we can run this file using node.js.

$ node src/index.js

This is exactly ts-nodewhat is done behind the scenes, but it does not generate a JavaScript file.

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

$ tsc --watch -p .

Where the generated JavaScript files are placed depends on the outDir option in the tsconfig.json file.

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

We can run any .js file using node myFile.js and any TypeScript file using npx ts-node myFile.ts .

请注意, we used npx when running the ts-node command.

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

If the package is not installed, npx will download and run it.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Setting optionals with default values in React TypeScript

Publish Date:2025/03/15 Views:70 Category:React

Make optionals with default values ​​in React TypeScript: Use a question mark to mark properties on a type as optional. Provide default values ​​for properties when destructuring them in the function's definition.

Qualify useState as an object in React TypeScript

Publish Date:2025/03/08 Views:117 Category:React

To qualify the useState hook object in React, use the hook's generic type, for example, const [employee, setEmployee] = useState{name: string; salary: number}({name: ,salary: 0}) . The state variable will only accept key-value pairs of the specified t

Adding Types to Refs in React with TypeScript

Publish Date:2025/03/08 Views:137 Category:React

Adding types to the useRef hook might be a little confusing at first, let’s look at why that is: import {useEffect, useRef} from react ; export function RefDemo () { const inputRef = useRef (); useEffect ( () = { inputRef. current . fo

How to pass functions as props in React TypeScript

Publish Date:2025/03/06 Views:114 Category:React

Passing functions as props in React TypeScript: Define the type of the function property in the component's interface. Define the function in the parent component. Pass the function as a prop to the child component. interface ButtonProps { sum : ( a:

Type useState as array of strings in React TypeScript

Publish Date:2025/03/05 Views:191 Category:React

To type the useState hook in React as an array of strings, use the hook's generics, such as const [names, setNames] = useStatestring[]([]) . State variables can be initialized to an empty array or an array of strings, and only accept string values.

Type useState as array of objects in React TypeScript

Publish Date:2025/03/05 Views:161 Category:React

To type the useState hook as an array of objects in React, use the hook's generics, such as const [employees, setEmployees] = useState{salary: number; name: string}[]([]) . The state variable can be initialized to an empty array and only accepts point

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial