Skip to content

Latest commit

 

History

History
130 lines (90 loc) · 2.79 KB

node-js-typescript.md

File metadata and controls

130 lines (90 loc) · 2.79 KB

New Node.JS + TypeScript + Express project cheat sheet

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

Initializing Node.js Project

  1. Run npm init -y inside the desired project folder
  2. You may also fill in the project details under package.json

Installing TypeScript

  1. Install TypeScript as a dev dependency by running:
npm install -D typescript ts-node-dev
  1. Now run the following to initialize TypeScript:
npx tsc --init
  1. The previous command will generate a tsconfig.json, open it, and uncomment and set the rootDir and outDir as your like. outDir is where the converted files will sit. Example:
"rootDir": "./src",
// ...
"outDir": "./dist", 

Installing eslint

Source: eslint

  1. Run:
npm i --save-dev eslint

Installing lint preset

Source: eslint-config-rocketseat

  1. Run:
npm i -D eslint @rocketseat/eslint-config
  1. Create .eslintrc.json file in the root of the project and set it up as follows:
{
  "extends": "@rocketseat/eslint-config/node"
}

Installing nodemon

  1. Install nodemon by running:
npm i --save-dev nodemon
  1. 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"
}

Installing express

Source: express

  1. Install express by running:
npm i express
  1. Install express types by running:
npm install -D @types/express

Testing

  1. 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}`)
})
  1. Test build, start and dev scripts

Connecting to a GitHub repository

  1. Run git init
  2. Create the .gitignore file
  3. Paste into the .gitinore the contents of this template
  4. Create the repository and add remote origin
  5. Create the first commit and push files