83

I'm very confused about the difference between tsc and ts-node. I'm learning TypeScript and I usually transpile server .ts files with tsc command.

Now, I'm approaching nestjs framework, and I see that it uses ts-node.

So what's the difference between the two? Which one should I use?

gremo
  • 47,186
  • 75
  • 257
  • 421
  • @jfriend00 can you elaborate your answer? AFAIK tsc will change `import` to the commonjs `require()`, which in turn will load the JavaScript source file (assuming in node_modules). – gremo Jul 20 '18 at 18:29
  • 1
    Read the first paragraph here: https://www.npmjs.com/package/ts-node – jfriend00 Jul 20 '18 at 18:40
  • Some discussion at https://www.reddit.com/r/typescript/comments/8vkvzy/typescript_with_node_should_i_use_tsnode_or_tsc/ – Michael Freidgeim Oct 10 '19 at 21:27

2 Answers2

63

The main difference is that tsc transpile all the file according to your tsconfig.

Instead, ts-node will start from the entry file and transpile the file step by step through the tree based on the import/export.

Ambroise Rabier
  • 3,636
  • 3
  • 27
  • 38
Adrien De Peretti
  • 3,342
  • 16
  • 22
19

Most common practice is that tsc is used for production build and ts-node for development purposes running in --watch mode along with nodemon. This is a command i often use for development mode for my node/typescript projects:

"dev": "nodemon -w *.ts -e ts -x ts-node --files -H -T ./src/index.ts"
lukasz
  • 867
  • 1
  • 8
  • 17
  • `tsc --watch` could've also been used which uses incremental builds and is faster than `nodemon`.. – yakya Mar 14 '22 at 09:17
  • I'm wondering which builder should I use to build react applications nowadays – Es Noguera Mar 29 '22 at 13:54
  • 1
    Quick update on this, ts-node CAN be used in production: https://stackoverflow.com/questions/60581617/is-it-a-bad-practise-to-use-ts-node-in-production and there's a faster way than using nodemon: https://www.npmjs.com/package/ts-node-dev – Molten Ice Apr 08 '22 at 15:18
  • `Quick update on this, ts-node CAN be used in production` Yeah no one said it can't, it's just the most common practice. – lukasz Sep 06 '22 at 07:09
  • 1
    thank you for a working sample script in package json – hkong Sep 24 '22 at 18:29
  • what is the role of nodemon, if ts-node also have the --watch? – g.pickardou Mar 13 '23 at 04:22