Cookie Consent by Free Privacy Policy Generator ๐Ÿ“Œ Effortlessly Setting up Your React Project with Vite, Husky, TypeScript, and ESLint: A Comprehensive Guide

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š Effortlessly Setting up Your React Project with Vite, Husky, TypeScript, and ESLint: A Comprehensive Guide


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

What we will learn

This blog post provides a step-by-step guide to set up a React project using Vite, Husky, TypeScript, and ESLint.

It explains the benefits of each tool and their role in ensuring a smooth and efficient development process.

The post includes code snippets and screenshots to illustrate each step of the setup process, making it easy for readers to follow along.

To get The full code go to https://github.com/pappijx/Vite-react-eslint-prettier-husky-setup

Prerequisites

Make sure you have Node.js installed on your machine. You can download it from the official website (https://nodejs.org/).

Setup Guide

1. Generate Vite + Typescript App

-> After this you will be asked to enter your project name

terminal> npm create vite@latest
? Project name: ยป test-project

-> Next you need to select the framework of your choice
-> Select react here

? Select a framework: ยป - Use arrow-keys. Return to submit.
    Vanilla
    Vue
>   React
    Preact
    Lit
    Svelte
    Others

-> Next you need to add typescript to you project

? Select a variant: ยป - Use arrow-keys. Return to submit.
    JavaScript
>   TypeScript
    JavaScript + SWC
    TypeScript + SWC

-> After this you will see the final config as

terminal> npm create vite@latest
โˆš Project name: ... test-project
โˆš Select a framework: ยป React
โˆš Select a variant: ยป TypeScript

Scaffolding project in D:\Work\Blogs\test-project...

Done. Now run:

  cd test-project
  npm install
  npm run dev

-> To run the app we need to install dependencies, so just do

terminal> npm i

-> After this you project directory and package.json looks like this

Image description

2. Setup eslint

-> First we need to install eslint as dev dependency

terminal> npm i -D eslint

-> Now we need to add a basic config file for eslint, for that

terminal> npx eslint --init

// this generates 

You can also run this command directly using 'npm init @eslint/config'.
Need to install the following packages:
  @eslint/create-config
Ok to proceed? (y)

-> Just say y (yes) here and proceed. After this we need to select the way we want to use eslint

? How would you like to use ESLint? ... 
  To check syntax only
> To check syntax and find problems
  To check syntax, find problems, and enforce code style

-> I will select the second option for now, and we will use airbnb style guide for eslint afterwards.

-> After this choose module type for your project, I will choose Javascript modules

? What type of modules does your project use? ...
> JavaScript modules (import/export)
  CommonJS (require/exports)
  None of these

-> Next choose framework as react

? Which framework does your project use? ...
> React
  Vue.js
  None of these

-> Next let the wizard know that we are using typescript

? Does your project use TypeScript? ยป No / Yes
// select yes and hit enter

-> Next select the platform where the will run, select browser and hit enter

? Where does your code run? ...  (Press <space> to select, <a> to toggle all, <i> to invert selection)
> Browser
  Node

-> Now select the .eslintrc file extension, I will go with javascript format.

? What format do you want your config file to be in? ...
> JavaScript
  YAML
  JSON

-> Now the wizard will ask you for permission to install some dependencies, hit enter and proceed

The config that you've selected requires the following dependencies:

eslint-plugin-react@latest @typescript-eslint/eslint-plugin@latest @typescript-eslint/parser@latest
? Would you like to install them now? ยป No / Yes  

-> And last question is, which package manager are you using? I am using npm, you can choose your package manager.

? Which package manager do you want to use? ... 
> npm
  yarn
  pnpm

-> Hitting enter initiates deps installation. And your root directory will have .eslintrc.cjs file with some basic settings.

-> Now your package.json and .eslintrc files will look something like below

Image description

*There is an error in the *

-> Now we need to setup a eslint style guide in our project, I am using airbnb() as the base style.
This helps a developer to write proper and clean code.

So we run below command to get all the dependencies required by eslint airbnb style guide.

terminal> npx install-peerdeps --dev eslint-config-airbnb
Need to install the following packages:
  install-peerdeps
Ok to proceed? (y) y

-> Insert y and hit enter. Now your devDependencies looks like below.

"devDependencies": {
    "@types/react": "^18.0.28",
    "@types/react-dom": "^18.0.11",
    "@typescript-eslint/eslint-plugin": "^5.57.0",
    "@typescript-eslint/parser": "^5.57.0",
    "@vitejs/plugin-react": "^3.1.0",
    "eslint": "^8.2.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-plugin-import": "^2.25.3",
    "eslint-plugin-jsx-a11y": "^6.5.1",
    "eslint-plugin-react": "^7.28.0",
    "eslint-plugin-react-hooks": "^4.3.0",
    "typescript": "^4.9.3",
    "vite": "^4.2.0"
  }

-> Just add "airbnb", "airbnb/hooks" in your .eslintrc files extends key. Now your .eslintrc files looks like below

Image description

-> we need to add typescript support for eslint-airbnb

terminal> npm install eslint-config-airbnb-typescript

-> After this add 'airbnb-typescript' to your .eslintrc extends array.

-> After this we need to add project: './tsconfig.json' to out .eslintrc parserOptions.

-> You have a different error at top line in .eslintrc file as shown below.

Image description

-> This is because our .eslintrc file is not looked up by tsconfig file. So we need to add this in tsconfig.json, like below

Image description

-> After this open any tsx file, you will see blood all over the screen. This is because we are following airbnb guide for javascript.

Let open app.tsx file and remove all not so important code. So now the file looks like below with an error.

Image description

The error says

'React' must be in scope when using JSXeslint

This error occurs because airbnb style guide forces us to import React from 'react' as in old react this was a requirement, but in new versions of react we don't need this.

To escape this rule we will add exception in .eslintrc file in rules array.

module.exports = {
  env: {
    browser: true,
    es2021: true,
  },
  extends: [
    'airbnb',
    'airbnb-typescript',
    'airbnb/hooks',
    'plugin:react/recommended',
    'plugin:@typescript-eslint/recommended',
  ],
  overrides: [],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module',
    project: './tsconfig.json',
  },
  plugins: ['react', '@typescript-eslint'],
  rules: {
    'react/react-in-jsx-scope': 0,
  },
};

Thats it for eslintrc file.

3. Add Prettier

-> We need prettier to format our code properly so that it it more readable and so everyone use similar code formatting

-> So now just install all prettier dependencies

terminal> npm i -D prettier eslint-config-prettier eslint-plugin-prettier

-> Now create .prettierrc.cjs file in root directory

module.exports = {
  trailingComma: "es5",
  tabWidth: 2,
  semi: true,
  singleQuote: true,
};

Thats all for prettier!!!

4. Add husky to project
-> Now, this is the easiest thing to do but the most important. Using husky you make your team follow the guidelines specified by you and that makes code cleaner and helps make good coding implementations.

-> To install husky just do

npx mrm@2 lint-staged
npm i

and then just add the below lines to your package.json.

"lint-staged": {
        "*.{js,css,ts,tsx,jsx}": [
"prettier --write", "eslint --fix"]
    }

Keep in mind to add eslint after prettier else it both will conflict and husky generates error

Thats it, You now have a well structured project with prettier, eslint, husky and vite.

...



๐Ÿ“Œ Effortlessly Setting up Your React Project with Vite, Husky, TypeScript, and ESLint: A Comprehensive Guide


๐Ÿ“ˆ 146.69 Punkte

๐Ÿ“Œ Guide to Setting Up Prettier, Airbnb ESLint, and Husky for Your Next Project


๐Ÿ“ˆ 74.06 Punkte

๐Ÿ“Œ Setup React Typescript with Vite & ESLint


๐Ÿ“ˆ 60.09 Punkte

๐Ÿ“Œ Setting up a React project using Vite + TypeScript + Vitest


๐Ÿ“ˆ 55.38 Punkte

๐Ÿ“Œ React.js Vitest Unit Testing (Husky, lint-staged, ESLint, Prettier)


๐Ÿ“ˆ 54.84 Punkte

๐Ÿ“Œ Git Project Configuration With Husky and ESLint


๐Ÿ“ˆ 53.81 Punkte

๐Ÿ“Œ Building a Modern React App with Vite, ESLint, and Prettier In VSCode


๐Ÿ“ˆ 49.81 Punkte

๐Ÿ“Œ Effortless Testing Setup for React with Vite, TypeScript, Jest, and React Testing Library


๐Ÿ“ˆ 49.77 Punkte

๐Ÿ“Œ Setting up Code Formatting with ESLint, TypeScript, and Prettier in Visual Studio Code


๐Ÿ“ˆ 44.96 Punkte

๐Ÿ“Œ Configuring Prettier and ESLint in Your VSCode TypeScript Project


๐Ÿ“ˆ 44.23 Punkte

๐Ÿ“Œ React Monorepo Setup Tutorial with pnpm and Vite: React project + UI, Utils


๐Ÿ“ˆ 43.69 Punkte

๐Ÿ“Œ Setting Up Express development environment (Typescript, Eslint, Prettier)


๐Ÿ“ˆ 43.18 Punkte

๐Ÿ“Œ Create a project in React without create-react-app/vite 2023 (Spanish)


๐Ÿ“ˆ 41.91 Punkte

๐Ÿ“Œ Why I choose CRA ( Create-react-app) over Vite for this React project ?


๐Ÿ“ˆ 41.91 Punkte

๐Ÿ“Œ Build a Profit Margin Calculator with Vite.js + React.js, TypeScript and Tailwind CSS


๐Ÿ“ˆ 40.97 Punkte

๐Ÿ“Œ Getting started with React.js with Typescript using vite.jsโšก


๐Ÿ“ˆ 39.18 Punkte

๐Ÿ“Œ Elevating Your React A Comprehensive Guide to Using React-Select


๐Ÿ“ˆ 39.1 Punkte

๐Ÿ“Œ Level Up Your TypeScript Projects: Discover the Power of ESLint and Prettier


๐Ÿ“ˆ 38.24 Punkte

๐Ÿ“Œ Introduction to Testing React Components with Vite, Vitest and React Testing Library


๐Ÿ“ˆ 37.71 Punkte

๐Ÿ“Œ Optimizing Code Quality: A Guide to Using Husky and Lint-Staged in Your Development Workflow


๐Ÿ“ˆ 36.95 Punkte

๐Ÿ“Œ Webentwicklung: Build-Tool Vite.js 5.1 experimentiert mit Vite Runtime API


๐Ÿ“ˆ 36.64 Punkte

๐Ÿ“Œ Webentwicklung: Build-Tool Vite.js 5.1 experimentiert mit Vite Runtime API


๐Ÿ“ˆ 36.64 Punkte

๐Ÿ“Œ Why Vite is the best? Advanced Features of Vite


๐Ÿ“ˆ 36.64 Punkte

๐Ÿ“Œ How to Deploy Your React or Vite Project on GitHub Pages using gh-pages


๐Ÿ“ˆ 36.6 Punkte

๐Ÿ“Œ This Week In React #143: RSC Quiz, useFormStatus, Panda CSS, useLayoutEffect, Million.js, Super Apps, React-Three-Fiber, Vite...


๐Ÿ“ˆ 35.92 Punkte

๐Ÿ“Œ This Week In React #143: RSC Quiz, useFormStatus, Panda CSS, useLayoutEffect, Million.js, Super Apps, React-Three-Fiber, Vite...


๐Ÿ“ˆ 35.92 Punkte

๐Ÿ“Œ This Week In React #174: ReactLabs, React-Strict-DOM, Remix, RNGH, Expo, RN+TV, TC39, Vite...


๐Ÿ“ˆ 35.92 Punkte

๐Ÿ“Œ Husky and lint-staged for pre-commit in React


๐Ÿ“ˆ 35.72 Punkte

๐Ÿ“Œ Transform Your Codebase with Prettier: A Guide with Husky Integration


๐Ÿ“ˆ 35.17 Punkte

๐Ÿ“Œ How to Setup React and Tailwind CSS with Vite in a Project


๐Ÿ“ˆ 34.89 Punkte

๐Ÿ“Œ My takes on EsLint and Prettier against TypeScript


๐Ÿ“ˆ 34.75 Punkte

๐Ÿ“Œ Setup Node with Typescript and eslint


๐Ÿ“ˆ 34.75 Punkte

๐Ÿ“Œ Setup path aliases in React + Vite + TS Project ๐Ÿค“


๐Ÿ“ˆ 33.11 Punkte

๐Ÿ“Œ Digging Deeper with TypeScript: ESlint


๐Ÿ“ˆ 32.96 Punkte

๐Ÿ“Œ Vite vs Webpack: Which One and Why for Your Next React App (The Battle of Bundlers)


๐Ÿ“ˆ 32.4 Punkte











matomo