Reputation: 31
I followed a tutorial where it was mentioned that I don't need any extra configuration to run TypeScript code. I have Node.js version 20, TypeScript version 5.6.2, and ts-node 10.9.2 installed globally. I also installed the Code Runner and JavaScript/TypeScript Nightly extensions in VSCode. I created a simple TypeScript file (index.ts) and tried to run it using Shift+Enter in Code Runner. However, I encountered an error related to ES modules and Unexpected token 'export' in the console. I'm unsure what is causing this issue.
I installed Node.js, TypeScript, and ts-node, as suggested in the tutorial, and tried running a simple TypeScript file in VSCode using Code Runner. I expected the code to run without errors and print John in the console. Instead, I received an error stating I need to set "type": "module" in the package.json or use an .mjs file extension, as well as an Unexpected token 'export' error.
at TimeStamp 11:34 he mentioned no need of any compiler or extra file to run the typescript code.
I followed along all the steps
node was already installed version 20
npm install -g typescript
npm install -g ts-node
Typescript Version 5.6.2.
ts-node 10.9.2
in vscode installed Code Runner* and *JavaScript and TypeScript Nightly Extensions
Create index.ts file
type Person = {
name: string;
};
const person: Person = {
name: "John",
};
console.log(person.name);
Changed the VSCODE Keyvoard shortcut for Code Runner to Shift+Enter
Shift+Enter to Run the code using Code Runner
Output shows this ERROR:
(node:11032) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use node --trace-warnings ... to show where the warning was created)
c:\Users\faroo\Desktop\ts-playground\tempCodeRunnerFile.ts:5
export {};
^^^^^^
SyntaxError: Unexpected token 'export'
What I am doing wrong?
I also checked in to it and tried to create a tsconfig.json
file in the same directory as index.ts
file is
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
After that when I run index.ts file using code runner or terminal command **ts-node index.ts**
it runs with no errors.
My point and Quesion is that how is he doing without tsconfig.json file.
Upvotes: 2
Views: 243
Reputation: 347
Idk if i come late to the party and you already figured out the answer by yourself, but i was having the same problem a few minutes ago and since i found your thread while i was looking for the solution, i come back to answer it with the solution that worked for me.
the problem was ts-node
itself, it seems that doesn't support ESM modules
and there are some symbols/tokens that doesn't know how to parse correctly.
So, instead of using ts-node
i ended up replacing it with tsx
, that seems to have better support.
All you need to do is to install it globally like you did with ts-node
:
npm install -g tsx
then go and open your VScode's settings.json file:
ctrl + shift + p
-> Preferences: Open User Settings (JSON)
look for the code-runner.executorMap
property and change its typescript
value for this npx tsx
.
It should look something like this:
"code-runner.executorMap": {
"typescript": "npx tsx",
}
and that's it, now you should be able to run your TS scripts as i did without creating the tsconfig.json
file.
Upvotes: 0