Express + Typescript: TSLint, Nodemon and TS Node
Last time we got a simple Hello, World service running with Express and Typescript. It was a good start, but with the help of a few packages we can do a lot to speed up development.
Start by installing these package
yarn add --dev tslint nodemon ts-node
ts-lint
is a static analysis tool. We’ll use it to catch errors early.nodemon
is a utility that monitors for changes and automatically restarts your server.ts-node
is a Typescript execution environment for node.js
Now that these are installed, we can configure them with a couple files and a few commands.
Runng yarn tslint --init
will create a tslint.json file in the root of your project. There are a lot of rules that you can configure, but for now we’ll accept the ones tslint recommends for us so that we don’t have to edit this file any further. Check out the TS Lint site for documentation.
Now that you have a configuration, you can lint your project by running yarn tslint --project tsconfig.json
. If you’re like you there are already a few issues to fix. The output on my project tells me that I should be using single quotes instead of double and console.log isn’t allowed. My output looks like this:
yarn run v1.15.2
$ ~/Projects/express/helloworld/node_modules/.bin/tslint --project tsconfig.json
ERROR: ~/Projects/express/helloworld/src/server.ts:1:26 - ' should be "
ERROR: ~/Projects/express/helloworld/src/server.ts:6:9 - ' should be "
ERROR: ~/Projects/express/helloworld/src/server.ts:6:37 - ' should be "
ERROR: ~/Projects/express/helloworld/src/server.ts:7:24 - Calls to 'console.log' are not allowed.
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I’ll fix the quotes, but for now I want to keep my console.log so I’ll insert a // tslint:disable-next-line: no-console
comment on the line before it to tell TSLint to ignore the waring in on the next line.
Next lets configure nodemon to restart our server when we make changes. To do this, create another new file in the root of your project called nodemon.json
with the following contents:
{
"watch": ["src"],
"exec": "yarn serve",
"ext": "ts"
}
This file tells nodemon to watch for changes to any file with a ts
extension in the src
folder and execute yarn serve
when those change happen.
Now we have to add some scripts to our package.json
to finish up.
"scripts": {
"lint": "tslint --project .",
"build": "tsc",
"serve": "ts-node src/server.ts",
"start": "nodemon"
},
At this point we start the server with yarn start
. This tells nodemon to start the server and watch for changes. When we make a change, nodemon will call yarn serve
which executes ts-node src/server.ts
to run our server.
For consistency, I like to use yarn to run all of my commands so I’ve included easy short cuts yarn build
and yarn lint
to run the Typescript compilation and TSLint.
With these steps complete you have a working set up to build your Express application. All of this should be easily transferable to other node frameworks like Koa or Hapi, just make sure that you are installing the types for the frameworks you’re using.