This is the first installment in a series of guides showing how to create a Node.js + TypeScript API. The initial pieces will be very simple, but as we progress, I will be showing how to add features that would be expected in a production-quality API. You can find the repo for this project here: https://github.com/JWLangford/node-typescript-api.
You can also find a video walkthough here: https://youtu.be/SNAUpMJ4AWo
This installment will cover the installation and setup of the project.
Before going further, make sure you have these installed on your machine.
https://nodejs.org/en/Command Check: node -vYarn
npm install -g yarnCommand Check: yarn -vTypescript
npm install -g typescriptCommand Check: tsc -v
To get started, create the directory your API will inhabit.
Next, setup your project with yarn
yarn init -y
This will add a package.json file to your directory.
We’re going to need three external packages to get started.
- typescript: This package enables us to use types and type checks in our codebase
- express: This package is a minimal and flexible Node.js web application framework
- @types/express: This package contains types for the exported modules within the express package.
yarn add typescript @types/express -Dyarn add express
The next thing we need to do is configure TypeScript for our project. Run the following command inside the root folder of your project.
This will add a tsconfig.json file to your project. Go into the file and uncomment and update the