Atila Ferreira
Atila Ferreira

Reputation: 41

How to configure coderunner extension to execute ts

i understand that's a rookie question, but i failed to find an answer to it.

For making my tests easier I need to run small parts of my code and, after some search, I found the code runner extension for VsCode to do it. An example is the following:

import { getContractAddress } from './useContract';
const contractAddress = getContractAddress(1);
console.log({ contractAddress });

For some conceptual reason, this file is triggered hourly and changing that is tricky. So, I just need to test this 3 lines for checking the value that's returned.

I assumed this should be really easy but I'm always having errors

[Running] ts-node "/Users/guest/Documents/GitHub/frontend/src/tempCodeRunnerFile.ts" /opt/homebrew/lib/node_modules/ts-node/src/index.ts:859
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^ TSError: ⨯ Unable to compile TypeScript: src/tempCodeRunnerFile.ts(1,1): error TS1208: 'tempCodeRunnerFile.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.

Can you explain how to configure coderunner to run ts? I found several stuff explaining it to JS, but nothing to TS. I admit I'm newbie in web dev and coderunner, so don't be afraid of over explain it.,

Thanks!

Upvotes: 3

Views: 3964

Answers (3)

Akash Halder
Akash Halder

Reputation: 1

To solve this problem The easiest way is to :

  1. in the settings search code runner.
  2. select "Edit in settings.json."
  3. code-runner.executorMap": { "typescript": "tsc $fullFileName",}
  4. This will generate a .js file...Just run it with node.js.

Upvotes: 0

GollyJer
GollyJer

Reputation: 26762

Bun treats TypeScript as a first-class citizen.

The easiest way is to...

  • install bun
  • set bun as the executor for code-runner typescript.
    "code-runner.executorMap": {
      "typescript": "bun $fullFileName",
    },
    

Upvotes: 4

NeosCode
NeosCode

Reputation: 11

Yes you need to install the ts-node globally using this command but the tasks.json file in the .vscode folder and tsconfig.json file in the folder location of your typescript files is also required to auto-compile so to achieve this.

Note : First Install the Code Runner Extesnion in the VSCode which you can find in the VSCode or here in this marketplace link

  1. run this on CMD line in VSCode Folder wherever your typescript files needed to be run you can see point 2 on how to run this cmd -> tsc --init

  2. to run above command you can right click on one of the file in a folder where all your typescript files are present and 'open integrated terminal' and in the opened terminal you can run the command -> tsc --init

  3. Due to above command a file named tsconfig will be created in the folder where you run this command

  4. Open this tsconfig file and select the 'sourcemap' settings in this file , it will be commented just remove the '//' comment before sourcemap so it will get used

  5. Now click on the terminal option in the vscode at the top and select 'configure default build task' to compile the TS files in the background

  6. after selecting 'configure default build task' select the 'tsc:watch -tsconfig.json' option and the 'tasks.json' will appear in the window, also the file explorer will now show the .vscode folder with the tasks.json file

  7. Now install the ts-node using the following command in the terminal and the command is -> npm install -g ts-node

  8. Now the ts-files are ready to be run by the code runner, so whenever you right click anywhere inside the TS file in Vscode it will show a 'Run' option at the top in the option list select this an run the TS file.

Upvotes: 1

Related Questions