Home > Net >  Is there a need to compile my backend typescript code for production?
Is there a need to compile my backend typescript code for production?

Time:11-23

I know the title looks stupid but hear me out, if we have a typescript file that will not run on the browser then why is there a need to compile it? I mean it already runs without compiling atleast in development so theres no need to compile it.

Maybe theres something on the production side of things that I am not aware of but to my current knowledge, theres no need to compile it.

CodePudding user response:

I mean it already runs without compiling atleast in development so theres no need to compile it.

That assumption is not accurate.

Running in development, no matter whether it is Nest or ts-node or deno all transpile the TypeScript to JavaScript behind the scenes:

[ts-node] JIT transforms TypeScript into JavaScript, enabling you to directly execute TypeScript on Node.js without precompiling. ts-node Readme

At a high level, Deno converts TypeScript (as well as TSX and JSX) into JavaScript. Deno docs

And while I couldn't find anything in the Nest Docs on this topic, the typescript package is a direct dependency of the Nest CLI indicating that it's not just used for development (but also in the CLI itself to compile the project) and a rough glance through the codebase of the CLI also makes it look like the project is being compiled (either by Webpack or TS) prior to being executed.


To answer the question in the title: No, not necessarily. I have run a project in production with ts-node in the past.

Would I recommend it? No, not really. It's JIT compiled, meaning while the process executes stuff, it trims out stuff on the fly before it's being executed, not really helping performance. In development, it doesn't matter and ts-node-dev with its watch feature is incredibly useful, not having to wait for full compilation of the project but for production, having JavaScript there instead of TypeScript is a good idea, eventhough not strictly required.

  • Related