This cheat sheet depicts some broad instructions on how to set up a new Node.JS + TypeScript + Express project. It's suited for those who already have some experience and just want a simple checklist for a brand new project.
This cheat sheet will guide you through:
- Initializing a Node.js project
- Installing TypeScript
- Installing eslint
- Installing lint preset
- Installing nodemon
- Installing express
- Testing
- Connecting to a GitHub repository
- Run
npm init -y
inside the desired project folder - You may also fill in the project details under
package.json
- Install TypeScript as a dev dependency by running:
npm install -D typescript ts-node-dev
- Now run the following to initialize TypeScript:
npx tsc --init
- The previous command will generate a
tsconfig.json
, open it, and uncomment and set therootDir
andoutDir
as your like.outDir
is where the converted files will sit. Example:
"rootDir": "./src",
// ...
"outDir": "./dist",
Source: eslint
- Run:
npm i --save-dev eslint
Source: eslint-config-rocketseat
- Run:
npm i -D eslint @rocketseat/eslint-config
- Create
.eslintrc.json
file in the root of the project and set it up as follows:
{
"extends": "@rocketseat/eslint-config/node"
}
- Install nodemon by running:
npm i --save-dev nodemon
- Open
package.json
file and add the scripts for building, starting and debugging:
"scripts": {
"build": "npx tsc",
"start": "node dist/index.js",
"dev": "nodemon src/index.ts"
}
Source: express
- Install express by running:
npm i express
- Install express types by running:
npm install -D @types/express
- Create a file named
index.ts
under./src
and paste the following:
import express, { Request, Response, Application } from 'express'
const app: Application = express()
const port = process.env.PORT || 8000
app.get('/', (req: Request, res: Response) => {
res.send('Hello world')
})
app.listen(port, () => {
console.log(`Server is online -> http://localhost:${port}`)
})
- Test
build
,start
anddev
scripts
- Run
git init
- Create the
.gitignore
file - Paste into the
.gitinore
the contents of this template - Create the repository and add remote origin
- Create the first commit and push files