Run currently opened .ts file in VS Code

Press Ctrl+Shift+B shortcut and it runs in the terminal

Hyunbin
2 min readAug 18, 2022

Running a TypeScript file in the terminal is simple. npx ts-node script.ts is an example. However, typing the command is not convenient, especially when the file is nested deeply inside the tree. What if there is a way to open a .ts file in VS Code, and run it right away? What if there is a keyboard shortcut for it?

Running currently opened TypeScript file using a keyboard shortcut

There is a ${relativeFile} variable in VS Code, so opening the terminal and running npx esno ${relativeFile} is all that has to be done. Thankfully, this can done using a VS Code’s custom task. (esno is similar to ts-node)

‘Tasks: Run Task’ option is highlighted in a VS Code Command Palette list

Open a VS Code workspace and create a tasks.json file. The content can be found in the Gist. All comments and the "group": "build" can be removed.

.vscode/tasks.json file is opened in a code editor

Then open a TypeScript file, open the Command Palette, and select Task: Run Task / npx: esno. The file does not have to be a standalone file. Imports and exports can be used according to the tsconfig.json or other configurations.

Running currently opened TypeScript file using the Command Palette

If the "group": "build" configuration is set, the custom task can be triggered using the Run Build Task shortcut. (Ctrl+Shift+B in Windows)

--

--

Hyunbin

Node.js and web developer using TypeScript. Undergraduate in Seoul National University.