Reputation: 28500
Node is able to run the following without errors (node hello-world.ts
):
var f = () => {
console.log('Hello World!');
};
f();
However, when I try this file:
interface Accountable {
getIncome() : number;
}
I get get the following exception:
interface Accountable {
^^^^^^^^^^^
SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:414:25)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:136:18)
at node.js:963:3
I've tried adding --target ES5
and ES2015
to the TSC settings page, but no effect.
Upvotes: 1
Views: 2074
Reputation: 707148
Making my comments into an answer.
Node.js has no built-in support for TypeScript and no built-in support for automatically transpiling TypeScript into plain JavaScript. As such, you have to transpile your TypeScript files first before running them in node.js.
Your first code example works because it is legal ES6 JavaScript syntax and thus node.js can just run it directly.
Upvotes: 2
Reputation: 15
I've had the same problem using both VS Code and WebStorm. Thanks to cdbjorin I was able to make it work by changing one line in the Package.json file:
...
"name": "myProject",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./myScript.ts"// <--
},
...
to:
"start": "ts-node ./myscript.ts"
Or in this case simply use ts-node myScript.ts
to run it;
Hope this helps
Upvotes: 0