Logo

dev-resources.site

for different kinds of informations.

Setting Up Express development environment (Typescript, Eslint, Prettier)

Published at
4/25/2024
Categories
express
typescript
eslint
prettier
Author
muhammetandic
Author
13 person written this
muhammetandic
open
Setting Up Express development environment (Typescript, Eslint, Prettier)

I wanted to set up an Express development environment with latest version of NPM packages. But I came across some challenges. I solved all problems after long searches on the Internet. So I decided to write a short text to explain it to someone who needs it.

  1. initialize a node project.

    npm init
    
  2. install dependencies and dev dependencies

    npm i express
    npm i -D nodemon ts-node typescript @types/express @types/node
    
  3. create tsconfig.json file
    I want to use ESM modules in this project. So that set target ESNext and module NodeNext

    {
      "compilerOptions": {
        "target": "ESNext",
        "module": "NodeNext",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "outDir": "dist",
      },
      "include": ["src/**/*.ts"],
      "exclude": ["node_modules", "dist", "src/**/*.spec.ts"],
    }
    
  4. lets add scripts to package.json
    Working with ESM modules with ts-node is a bit tricky. To solve this problem I created a register.js file.

    import { register } from "module";
    import { pathToFileURL } from "url";
    
    register("ts-node/esm", pathToFileURL("./"));
    

    Then imported to node this register.js file. Also you can see .env files passed to node.

      "scripts": {
        "dev": "nodemon --exec node --import=./register.js --env-file=.env.development src/index.ts",
        "build": "tsc --project .",
        "start": "node --env-file=.env.production dist/index.js"
      }
    
  5. Add ESLint to project

    npx @eslint/create-config@latest
    

    These are my choices from wizard.

    √ How would you like to use ESLint? · problems
    √ What type of modules does your project use? · esm
    √ Which framework does your project use? · none
    √ Does your project use TypeScript? · typescript
    √ Where does your code run? · node
    The config that you've selected requires the following dependencies:
    
    eslint, globals, @eslint/js, typescript-eslint
    √ Would you like to install them now? · No / Yes
    √ Which package manager do you want to use? · npm
    

    This wizard created a eslint.config.js file. This file is new form of config file called flat config file.

    In VS Code, ESLint extension doesn't work with new flat config file. In Preferences you should change experimental flag true.

    Change Use Flat Config

  6. Lastly add Prettier

    npm i -D prettier eslint eslint-plugin-prettier
    

    Then create .prettierrc file.

    {
        "printWidth": 120,
        "tabWidth": 2,
        "useTabs": false,
        "semi": true,
        "singleQuote": true,
        "trailingComma": "all",
        "bracketSpacing": true,
        "endOfLine": "auto"
    }
    

    Some changes are required in the eslint.config.js file.

    import globals from 'globals';
    import pluginJs from '@eslint/js';
    import tseslint from 'typescript-eslint';
    import pluginPrettier from 'eslint-plugin-prettier';
    
    export default [
      { languageOptions: { globals: globals.node } },
      pluginJs.configs.recommended,
      ...tseslint.configs.recommended,
      {
        plugins: {
          prettier: pluginPrettier,
        },
        rules: {
          'prettier/prettier': 'error',
        },
      },
    ];
    

That's all. Thanks to read the article.

prettier Article's
30 articles in total
Favicon
VSCode Prettier format on save
Favicon
Beyond Spellcheck: How Static Analysis Tools Enhance Collaboration in Coding
Favicon
Setup Eslint Prettier in a TypeScript project with mongoose ODM
Favicon
Vue3 + ESLint 9.13.0 + Prettier +TypeScript and VSCode Autoformat on Save
Favicon
Configurando Prettier, ESLint y Husky en Angular
Favicon
Setup Prettier Pada Nextjs Tailwind Project
Favicon
Regras customizáveis para Prettier + Eslint em React
Favicon
Useful VS Code Extensions for JS
Favicon
ESLint x Prettier: The Right Way To Start A JavaScript Project
Favicon
How to Set Up ESLint and Prettier in a TypeScript Project
Favicon
Customizable rules for Prettier + Eslint in React
Favicon
Tired of Messy Code?🥴
Favicon
Streamline Your Tailwind CSS Workflow with Prettier Plugin Enhancements
Favicon
Why Do I Love Code Formatters?
Favicon
How to set up Eslint and prettier
Favicon
Configure Eslint, Prettier and show eslint warning into running console vite react typescript project
Favicon
Building Vue3 Component Library from Scratch #11 ESlint + Prettier + Stylelint
Favicon
Setup Eslint + Prettier for code standardization in React
Favicon
Setup Eslint + Prettier para padronização de código em React
Favicon
How to prevent Prettier putting a full stop on a new line after a link
Favicon
Moving "server-only" to the top of the imports
Favicon
My opinion about opinionated Prettier: 👎
Favicon
Setting Up Express development environment (Typescript, Eslint, Prettier)
Favicon
Biome.js : Prettier+ESLint killer ?
Favicon
Most elemental code formatting for Typescript/Javascript: .editorconfig vs prettier
Favicon
Setup Next.js 14 project with Eslint + Prettier + Tailwind CSS
Favicon
Prettier for PHP File Formats in VSCode
Favicon
Angular 14 + Prettier + Husky Setup
Favicon
แชร์ไฟล์ Prettier ง่ายๆ แค่ปลายนิ้ว!
Favicon
NextJS 14'te Kod Kalitesini Artırma: ESLint, Prettier ve Husky Kullanımı

Featured ones: