Search code examples
javascriptnode.jstypescriptexpressheroku

Deploy node.js / express / mongoDB ( with typescript ) in heroku


Before, in the backend i would not use typescript, i would just use pure javascript, but know, this is my first backend app with typescript.

I want to deploy of course this app to heroku, but, i wonder if in the procfile component i have to tell heroku to get the server running in the server.ts, or in the server.js in the dist folder.

What i'm trying to say, is that, should i compile my code to translate it into pure javascript in the dist folder in order to deplot, or is it okay just to just run the server in the server.ts file ?

This is how my project looks like

enter image description here


Solution

  • TypeScript is ultimately just there to help you structure your code properly, as it is meant to be compiled to JavaScript. While you can run your server via ts-node on a VM, it is simpler to just compile it to JS and run it on your VM, as at that point it is no different from a typical NodeJS application. Other advantages are smaller build files since your js files will take up less space and startup time.

    That being said there is no stopping you from using ts-node on heroku by putting ts-node index.ts inside your package.json start script, (also ts-node should be in dependencies not devDependencies), but personally I'd go down the compilation route as that is pretty much just another NodeJS application.

    See Heroku can't find ts-node