DEV Community

Aditeya Srivastava
Aditeya Srivastava

Posted on

Setup Node with Typescript and eslint

  • Create an empty directory for your project and move in it.
mkdir project
cd project
Enter fullscreen mode Exit fullscreen mode
  • Initialize npm
npm init -y
Enter fullscreen mode Exit fullscreen mode
  • Install typescript as a dev dependency.
npm i -D typescript
Enter fullscreen mode Exit fullscreen mode
  • Create a typescript config file
npx tsc --init
Enter fullscreen mode Exit fullscreen mode

the D flag installs it as a dev dependency

  • Install express (dependency) and it's types (dev dependency)
npm i express
npm i -D @types/express
Enter fullscreen mode Exit fullscreen mode
  • Create a src folder and create a new file 'index.ts' and copy the following code.
import express from "express";

const app = express();

app.get("/", (req, res) => {
    res.send("Hello World");
});

app.listen(5000, () => {
    console.log("server started");
});
Enter fullscreen mode Exit fullscreen mode
  • Open tsconfig.json and find "outDir", uncomment it, and update its value to dist
"outDir": "./dist"
Enter fullscreen mode Exit fullscreen mode
  • Install nodemon (dev dependency)
npm i -D nodemon
Enter fullscreen mode Exit fullscreen mode
  • Install ts-node (dev dependency)
npm i -D ts-node
Enter fullscreen mode Exit fullscreen mode
  • Open package.json update main to be
"main": "dist/index.js"
Enter fullscreen mode Exit fullscreen mode

and add the following under scripts

"start": "nodemon src/index.ts"
Enter fullscreen mode Exit fullscreen mode
  • Install eslint (dev dependency)
npm i -D eslint
Enter fullscreen mode Exit fullscreen mode
  • Create a eslint config file
npx eslint --init
Enter fullscreen mode Exit fullscreen mode

Choose all the config from the prompt

  • Add lint to script in package.json
"lint": "eslint . --ext .ts"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)