I am doing some code challenges using Typescript in VS code. When I try to run the code and see the output, I get "Code Language is not supported or defined". The language mode is set to Typescript React (I also tried just Typescript). And the file has a .tsx ending. Finally, I also did compile the file and make a duplicate .js version. Is there something I am forgetting?
CodePudding user response:
You cannot run a typescript file the way you would run a javascript file. You will need to first compile your typescript file into javascript with the command below in the directory containing your .ts
file:
npx tsc -w <your-file-name.ts>
The command above will create a <your-file-name.js>
file in the same directory and keep watching for any realtime changes with -w
option. Then you can run the .js
file in the same directory with either node or nodemon like this:
node <your-file-name.js>
---UPDATE---
Having to input these two commands mentioned above tsc -w
and node <your-file-name.js>
every time you might get cumbersome after a few times, you can use the ts-node
command which combines both of the commands stated above into one:
ts-node <your-file-name.ts>
CodePudding user response:
Run npx tsc -w And in another terminal run node filename.js
CodePudding user response:
Judging by the fact that your file ends with .tsx you are probably using React. I will also assume that you used create-react-app to init your project.
If both of my assumptions are correct, try:
npm start
If you are new to both React and Typescript, I would also suggest taking a look into that tool as it creates a very decent starting point for you to start learning both. https://create-react-app.dev/