100

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?

3
  • @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). Commented Jul 20, 2018 at 18:29
  • 1
    Read the first paragraph here: npmjs.com/package/ts-node Commented Jul 20, 2018 at 18:40
  • Some discussion at reddit.com/r/typescript/comments/8vkvzy/… Commented Oct 10, 2019 at 21:27

3 Answers 3

77

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.

4
  • 23
    So which one to use and when? Commented Jan 2, 2019 at 23:30
  • 4
    Its a better practice to use tsc to build your project and then run it with node in production, its also faster Commented Jan 3, 2019 at 7:37
  • 6
    Sometimes you only need to compile one entrypoint and it's imports - ts-node is perfect for that and has no downsides. Commented Dec 3, 2020 at 15:40
  • i think that there is a ts-node-dev package which speeds build times up if performance is an issue Commented Mar 12, 2022 at 13:58
26

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"
6
  • 1
    tsc --watch could've also been used which uses incremental builds and is faster than nodemon.. Commented Mar 14, 2022 at 9:17
  • I'm wondering which builder should I use to build react applications nowadays Commented Mar 29, 2022 at 13:54
  • 1
    Quick update on this, ts-node CAN be used in production: stackoverflow.com/questions/60581617/… and there's a faster way than using nodemon: npmjs.com/package/ts-node-dev Commented Apr 8, 2022 at 15:18
  • 1
    thank you for a working sample script in package json Commented Sep 24, 2022 at 18:29
  • 1
    what is the role of nodemon, if ts-node also have the --watch? Commented Mar 13, 2023 at 4:22
1

TS-Node can also act as a REPL environment along with compiling.

0

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.