Create an Express app with Typescript, Mocha, Eslint, Mocha, Istanbul, Nodemon and Prettier in 2 minutes

Daniel Gong
3 min readJul 1, 2020

--

Every time I start a new project, it always took me hours and hours and even days to do the initial setup, especially when typescript was involved.

Don’t let the initial setup be a blocker, you shouldn’t need to spend hours on things like how to get typescript work with mocha etc. Spend your time on the business logic of your app, let’s build the basic app structure in 2 minutes.

Use this magic command:

npx create-typescript-express-app <your-app-name>

This command will download the packages mentioned in the title and create a basic project structure for you.

If you have your own folder structure, just delete tests folder and everything in src folder except for src/server.ts

dist -- target folder for `npm build` (git ignored)
src -- source folder, all source code
src/middleware - you can put your common middleware here
src/middleware/cache-forever.ts - an example middleware which sets the Cache-Control header
src/routes - all urls available from the service
src/route/root - urls at root level
src/route/root/root.controler.ts - a controller for /favicon.ico /robots.txt /health-check endpoints
src/routes/users/users.controller.ts - an example controller
src/routes/users/users.service.ts - an example service/helper/model
src/utils - utitily helpers
src/utils/loggers.ts - winston logger
src/app.ts - creats an express object
src/config.ts - configuraiton file for the app
src/server.ts - start the http server
tests - test related files
tests/src - all tests
tests/utils.ts - helper function for tests
tests/setup.ts - test setup file
tmp - tmp folder for coverage output etc. (git ignored)
.env - environment variables for DEVELOPMENT environment

You also get some handy npm scripts

Development Commands

  • npm run dev - start the app in development mode (it reads the local .env file)
  • npm run test:dev - run tests locally (reads local .env)
  • npm run test:watch - run tests locally (reads local .env) in watch mode
  • npm run lint - run eslint
  • npm run format - run prettier
  • npm run build:dev - build typescript with source maps and comments in code are kept
  • npm run mocha - a helper npm script for running customised mocha command e.g. test a single file npm run mocha -- file-name-or-pattern

You need to create a.env file first for yoru environment variables.

Production Commands

  • npm start - start application
  • npm build - compile typescript with no source maps and comments are removed from ts files
  • npm test - run tests and coverage report

npm start does not read the .env file, it is only used for local development environment. Your env vars could be configured in your CI/CD process for your servers.

NODE_PATH

NODE_PATH used in scripts are for improving the readability of import statements e.g. Relative paths like import someModule from '../../../utils/module' can be written as import someModule from 'src/utils/module'

Quality Control

This project has been configured with three steps of code quality controls

Pre-Commit Hook

eslint --fix is triggered before each commit. This command tries to fix linting errors when it is possible.

eslint has been configured to also check and fix formatting errors detected by prettier. (https://prettier.io/docs/en/integrating-with-linters.html)

Pre-Push Hook

npm run test:dev is triggered before each push. Push will fail if tests fail or test coverage is below the threshold defined in ./.nycrc.

npm run audit is triggered before each push. Push will fail if there are vulnerabilities in dependencies. You should run npm audit fix to fix the vulnerabilities and commit the changes before you push again.

Debug Configurations for VS Code

Copy launch.json and tasks.json from the link below to your project’s .vscode folder.

https://github.com/coolgk/project-templates/tree/master/node-typescript/.vscode

That’s it!

Run “npm test” and read the specs to see what’s already baked in.
Run “npm run dev” to start the dev server and test the /health-check endpoint.

You can find the source code here:

https://www.npmjs.com/package/create-typescript-express-app

https://github.com/coolgk/project-templates/tree/master/express-typescript

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Daniel Gong
Daniel Gong

No responses yet

Write a response