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.
Tutorial Video Link
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:UsersfarooDesktopts-playgroundtempCodeRunnerFile.ts:5
export {};
^^^^^^
SyntaxError: Unexpected token 'export'
What I am doing wrong?
Note:
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.