Skip to content

dimkirt/typescript-node-boilerplate

Repository files navigation

Typescript Node Boilerplate

Boilerplate Node project using Typescript.

TSConfig

ESLint

You can read more on how to start with ESLint for Typescript and the list of rules below:

The recommended starting rule sets are, which are the ones used in this project are:

⚠️ Important ⚠️: We also mention that below in the Prettier section, but it's not suggested to use linters for formatting:

Neither typescript-eslint nor ESLint core enable any formatting-related rules in any recommended presets. However, some third party plugin configurations may still enable that bad practice.

To make sure that all formatting rules of your linter are off you can install eslint-config-prettier.

Prettier

By far the biggest reason for adopting Prettier is to stop all the on-going debates over styles.

Use Prettier to take care of formatting and linter to take care of code quality. Don't use linters for formatting because linting performance will take a big hit. From Typescript ESLint docs:

We strongly recommend against using ESLint for formatting. We strongly recommend using Prettier, dprint, or an equivalent instead.

Regarding Prettier options, the general philosophy is that there should only be a handful of options otherwise the debate shifts over to Prettier options instead of how to format.

Yet the more options Prettier has, the further from the above goal it gets. The debates over styles just turn into debates over which Prettier options to use. Formatting wars break out with renewed vigour: “Which option values are better? Why? Did we make the right choices?”

Because of that and since I don't have any strong objection with the Prettier default options there is no need for any overrides. Overrides can be done in the .prettierrc.json file.

EditorConfig

EditorConfig helps maintain consistent coding styles for multiple developers working on the same project across various editors and IDEs.

Githooks

To set up githooks we use husky. We use two hooks,

  • one before committing which makes sure we commit only linted code and uses prettier to format it before committing
  • and one before pushing that is going to run the tests and build the Typescript code.

Testing

We use mocha as our test runner, you can run all the tests by running:

npm run test

Serving the app locally

You can serve the app locally by running:

npm install
npm run dev

Node.js needs to be installed.

Docker

We use the distroless image from Google to have smaller and more secure images.

To build the container image:

docker build -t typescript-node-boilerplate .

To start a docker container:

docker run -p 3000:3000 typescript-node-boilerplate

CI

I use Github Actions for CI since it's extremely easy to set it up with GitHub. I use the official "Setup Node.js Environment" action