15

I am trying out TypeScript on Node.js. I have installed it using npm install typescript

When just run tsc:

:~$ tsc Version 1.0.1.0 Syntax: tsc [options] [file ..]

So, I am trying to run simple code: console.log('test'); which is not outputing nothing.

How can I output anyting in console from typescript running on node.js?

4 Answers 4

24

tsc is a compiler not a repl.

You need to write your typescript , compile it with tsc then execute it with nodejs.

Sign up to request clarification or add additional context in comments.

3 Comments

Right, I see that now. But I can write something like tsc test.ts | node test.js and have it executed immediately. Thanks.
@AndrijaCacanovic If you are using Bash you would only want to invoke node if the tsc command was successful: tsc test.ts && node test.js
The answer below works, by using ts-node NPM package. I just tried it and it's the correct answer.
11

With ts-node, which is a TypeScript execution environment, you can now execute TypeScript with node as you expected from the tsc compiler.

After installing ts-node you can run your example directly as

ts-node -e 'console.log("test")'

or you can run a TypeScript file as:

ts-node script.ts

Please find further details on the ts-node npm page

1 Comment

Use: ts-node -e 'console.log("test")' . Otherwise the nested single quotes confuse the command line.
2

You need to install this dev depending

npm i -D @types/node

1 Comment

I had meet the issue. It just because ts-node will compile ts code into memery and some modules may out of work , may be you can try "process.stdout.write('')" to instead.
1

After writing

> console.log('test');

Save this by example as test.ts

then run the command tsc test.ts (this will compile your code to JS) you will see a new file created by tsc with the name test.js

Now you can run your test by typing: node test.js.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.