How to Write Node.js Applications in Typescript
And make it easier to maintain them while creating fewer bugs
--
Ever since I first discovered TypeScript, I use it for all my JavaScript applications. There are just so many advantages to using TypeScript that you’re going to need a compelling argument to get me to write anything in Vanilla JavaScript.
In this article, I’m going to show you how to set up a simple development environment for writing a Node.js application in TypeScript.
Let me start by saying that there probably are a thousand or more different ways you can create Node.js applications in TypeScript. I’m just going to show the way I like to do it myself.
If you don’t feel like reading this entire article, you can find my starter project here: https://github.com/FreekMencke/node-typescript-starter. It contains some extra features that we’re not going to discuss in this article.
Package.json
Like I said before, there are a lot of different ways you can do this. I like to use Webpack for my TypeScript projects. But first things first. Let’s start by creating a package.json
.
You can generate a package.json
by using the npm init
command or you can copy paste the code below and change it.
Let’s begin with a simple Webpack configuration for a JavaScript Node.js project. After I’ve completed the basic setup, I’ll add TypeScript.
The application
As mentioned before, we are going to start with a JavaScript application, and later we’ll convert this into TypeScript. Let’s create a src/
directory with a main.js
and information-logger.js
file with some Node.js functionality: