Run currently opened .ts file in VS Code
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?
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)
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.
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.
If the "group": "build"
configuration is set, the custom task can be triggered using the Run Build Task shortcut. (Ctrl+Shift+B
in Windows)