8

What is the best practice of deploying NodeJS server written in TypeScript to production?

I want to avoid having 3 files (ts, js, map) for every script in my git repository.

I could use grunt/gulp to create "dist" directory and copy all the compiled files there, but then I would have them in my repo anyways. I could create separate repo just for the compiled code, but thats not ideal as well I think.

Also, when I run node app.ts without the js or the map existing, it actually starts up everything fine. So are the compiled files even needed for node server?

Note: I am dont have any compilation script/task in place, my IDE is compiling the ts files automatically for me.

1 Answer 1

6

You can use typescript to do all of this for you

tsconfig.json

{
    ...
    "outDir": "dist",
    "sourceMap": false,
    "declaration": false
}
  • outDir will compile the files into a dist directory
  • sourceMap will determine whether to output .map files
  • declaration will determine whether to output .d.ts files

More options can be found here and information on using a tsconfig.json file can be found here

Sign up to request clarification or add additional context in comments.

5 Comments

but this would still require manual step wouldnt it? What I am looking for is to push *.ts to repo and then automatically pull it on server without me having to login and even better without relying on compile process on server. If that would fail then that would be trouble
Compilation is necessary at some point. Using a CI to compile and then deploy the compiled files to a server on success is common. Another approach is to use something like npm to publish only the compiled files to and then trigger a deploy onto the server that pulls from the npm repository. There is no magic way of doing what you are asking, you will need to setup an infrastructure path that will handle this for you. There are many guides and tutorials on this that you can find online, and the do not have to be TypeScript specific since after you compile, it's all just JavaScript afterall
Right, so the answer is pre-compiling the code on dev machine and pushing it to same repo or separate repo just for compiled code. Now how come that node app.ts is running fine without haveng the js files generadet?
You may not be using any typescript specific keywords. node does support a good bit of ES2015 so it would be happy with those, but should error on any typescript specific features.
It is true that tried it just with very basic node/express server most probably without anything typescript related. Thanks for the answer!

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.