TypeScript Node Starter Simplified
A simplified version of the official TypeScript Node Starter example.

I was settling down to write an article on GraphQL pagination with JavaScript / Node.js and found it disconcerting to not have a type system (been writing in TypeScript sufficiently long now to have made a mental switch). As it may be not be clear how to get up and running in this environment, I took a sidebar and wrote this article first.
As the official TypeScript Node Starter example is overly complicated, we will only use it as a guidepost and create an environment from scratch.
The completed solution is available for download.
As a pre-requisite, we need to have Node.js installed; install the latest long-term-support (LTS) version (as of this writing it is: v8.11.4 LTS).
In a new folder, enter and accept all the defaults:
npm init
Let us install the development dependencies:
npm install -D typescript
npm install -D tslint
npm install -D nodemon
In this simple example, we will implement a simple Express application; so we install the dependencies:
npm install express
npm install @types/express
We create the TypeScript configuration; based on the TypeScript Node Starter project (explained there).
tsconfig.json
Observations:
- The lib entry is not in the TypeScript Node Starter version
- The code in this article runs without adding it
- Went ahead and added it because I immediately had issues using configuration without it on other projects (including the GraphQL project)
- The lib configuration is List of library files to be included in the compilation; Adding es2015, then allows TypeScript to fully support ES2015. I assumed it already did.
We create the TSLint configuration using the command:
./node_modules/.bin/tslint --init
and we add the three rules, interface-name, no-console, and quotemark (self-explanatory).
tslint.json
We update the main and scripts sections of package.json:
package.json
Observations:
- During development, we use both npm run watch-ts (recompiles application on source changes) and npm run watch-node (restarts application on recompilation)
- npm run build-ts only compiles the application
- npm run serve (npm run start) only starts the application
With this in place we can create our Hello World Express application; the only change is we use import instead of require.
src / server.ts
To develop, we run:
npm run watch-ts
and in a separate terminal we run:
npm run watch-node
and then opening the url http://localhost:3000/ in a browser we can see the output of the application.
If we update the server.js source, the application is recompiled and restarted; reloading the browser will reflect the change.
Wrap Up
This article is nothing overly exciting; yet wanted to have a solid starting point for using TypeScript with Node.js that is closely aligned with the official example. Enjoy.
✉️ Subscribe to CodeBurst’s once-weekly Email Blast, 🐦 Follow CodeBurst on Twitter, view 🗺️ The 2018 Web Developer Roadmap, and 🕸️ Learn Full Stack Web Development.