Is it a bad practise to use ts-node in production?
Asked Answered
J

3

88

I'm currently using ts-node with express in production and it's working well so far. Is there any reason that I should compile and run .js instead?

Jeanett answered 7/3, 2020 at 19:50 Comment(3)
Performance, specifically startup time, will likely improve.Equilibrist
@AluanHaddad in my case ~200 project files it is only x2 better. Not sure is it worth considering additional problems like paths (tsconfig-paths)Suspire
In 2024, I'd recommend to use Bun or Deno. They have faster startup speed and lower memory usage compare to ts-node.Jeanett
I
109

According to Blake Embrey, the author of ts-node, you can use it in production BUT you should use it with the --transpile-only flag.

Example:

ts-node --transpile-only ./src/start.ts

If you are compiling with transpile only, it will have a lower memory footprint as no type information will be generated. However, it can come to problems when you are using reflect-metadata (with experimental support for decorators).

In short: Use ts-node --transpile-only (there is also ts-node-transpile-only) in production when you are not using reflect-metadata. Otherwise, use tsc in combination with classic node.

Iphigenia answered 19/1, 2021 at 10:25 Comment(1)
Linked discussion had a few more comments since this was posted, including a few benchmarks. According to them, ts-node uses significantly more memory than pure node, so I would say second answer is correct - it's better to compile beforehand.Insensible
P
26

No you shouldn't use it in production, even though it will cache the compiled files it'll be slower to start and consume more memory because it keeps around an instance of the compiler and files in memory.

It's better practice to compile the files beforehand with whatever build tooling you're using to make sure builds actually pass if for some reason your development environment and production might differ (this depends how your workflow looks like).

One case might be that you want to stop deployment if the app fails to actually build before deploying it and trying to run it and replace the running instance. An example might look like:

yarn
yarn build // tsc index.ts (possibly returns 1, stop build and output error)
yarn start // node index.js
Personate answered 7/3, 2020 at 23:20 Comment(1)
this is true, there are some instances where tsnode will not run but the tsc will not compile.Nikolaus
J
-13

You should build the server on your dockerfile and for development mix tsc + nodemon.

https://github.com/TypeStrong/ts-node As they said in their docs "Is currently experimental".

Jiggle answered 3/8, 2020 at 16:56 Comment(1)
Why are you assuming OP is using Docker?Urinary

© 2022 - 2024 — McMap. All rights reserved.