Skip to content

alexandervantrijffel/ts-node-project-template

Repository files navigation

Typescript Node project template

This template contains a full project configuration for Typescript and Node 18 based projects.

Features:

  • ESM module
  • build script that builds a single, minified, tree shaked .build/index.mjs bundle file that does not depend on external packages from node_modules. It comes with a Typescript sourcemap and is built with esbuild-kit/tsx
  • start:watch script for development mode, this auto rebuilds and restarts the app on changes withtsx watch
  • extensive lint configuration for standardized, consistent code
  • Husky configuration for validating commit messages, lint validation and running tests
  • tsconfig project configuration for Node 18 projects based on best practices for Node 18
  • jest configuration for unit tests, vitest for test execution vitest features
  • multi-staged Docker build with minimal Docker image size
  • Github Actions workflow for build, test and Docker build
  • both npm and yarn are supported

The build performance in development build mode increases significantly when using esbuild instead of the Typescript compiler tsc:

Getting started

With npm

Run npm install and npm run serve to run the application in development mode.

Run npm run test:watch to run unit tests in watch mode.

With yarn

Install yarn v1.22.19 or later.

Run yarn install and yarn serve to run the application in development mode.

Run yarn test:watch to run unit tests in watch mode.

Docker build

Build the Docker image with docker build . -t ts-node-project-template and run it with command docker run ts-node-project-template.

The resulting Docker image is only 30MB. This achieved by making use of a multi-stage Docker build and using m03geek/alpine-node:pico-14 as the base image for the production build. This doesn't include npm which saves 33MB and node is packed with upx. This leads to a Docker image that is around 88MB smaller compared to the official node:alpine-14 image.

Github actions

A Github Actions workflow is included that builds the package and runs the tests. When that succeeds a Docker image is built. Because node_modules are cached, both the build-test and build-docker jobs finish within a minute when the package dependencies can be retrieved from the cache.

Modify the workflow file at .github/workflows/main.yml for setting up pushing the Docker image to a registry.

Danger JS

With Danger JS checks are executed on PR's to validate the quality of the PR. The following checks are implemented in dangerfile.ts:

This is reported on the pull request by Danger JS as comments:

jHRgpCs

To allow Danger JS to add comments to the PR, take the following steps:

  • Create a Github Personal Access Token at https://github.com/settings/tokens/new with permission public_repo.
  • Add a secret to the repository with name DANGER_TOKEN and set the Personal Access Token as the value.

To extend dangerfile.ts with additional rules, make sure you add the danger package to the project as dev dependency and run danger locally on a pull request with the following command:

DANGER_GITHUB_API_TOKEN=<github_personal_access_token> yarn danger pr https://github.com/<username>/<repository>/pull/<pull request number>

Renovate

This template contain a starter configuration for !(Renovate)[https://github.com/renovatebot/renovate]. Renovate automates dependency updates by creating pull requests. Install the Renovate app for Github from !(here)[https://github.com/apps/renovate].