React Js Vite Commands Cheat Sheet

A cheat sheet for common React Vite app commands

Red Black Minimalist Cheat With The Letter Logo -iFwLqndMz8.png

npm create vite@latest my-react-app --template
npm create vite@latest

 

Project Setup

  1. Initialize a New Project:
    • npm init vite@latest my-project-name --template react
    • yarn create vite my-project-name --template react
    • pnpm create vite my-project-name --template react

Development

  1. Install Dependencies:

    • npm install
    • yarn
    • pnpm install
  2. Run Development Server:

    • npm run dev
    • yarn dev
    • pnpm dev

Build and Production

  1. Build for Production:

    • npm run build
    • yarn build
    • pnpm build
  2. Preview Production Build:

    • npm run preview
    • yarn preview
    • pnpm preview

Scripts (package.json)

 
{ "scripts": { "dev": "vite", "build": "vite build", "preview": "vite preview" } }

Vite Configuration

  1. Configuration File:
    • Vite configuration is typically done in vite.config.js or vite.config.ts.

Environment Variables

  1. Define Env Variables:
    • Create .env, .env.local, .env.production, or .env.development files.
 
VITE_API_URL=https://api.example.com

Adding Plugins

  1. Install Plugins:
    • Install the plugin via npm/yarn/pnpm and configure it in vite.config.js.

Example: Adding React Refresh

 
npm install @vitejs/plugin-react-refresh
// vite.config.js import reactRefresh from '@vitejs/plugin-react-refresh'; export default { plugins: [reactRefresh()] }

Common Dependencies

  1. React Router:

    • npm install react-router-dom
    • yarn add react-router-dom
    • pnpm add react-router-dom
  2. State Management (e.g., Redux):

  • npm install @reduxjs/toolkit react-redux
  • yarn add @reduxjs/toolkit react-redux
  • pnpm add @reduxjs/toolkit react-redux

Linting and Formatting

  1. ESLint:

    • npm install eslint
    • yarn add eslint
    • pnpm add eslint
    json
    { "scripts": { "lint": "eslint . --ext .js,.jsx,.ts,.tsx" } }
  2. Prettier:

    • npm install prettier
    • yarn add prettier
    • pnpm add prettier

Testing

  1. Testing Library:
    • npm install @testing-library/react
    • yarn add @testing-library/react
    • pnpm add @testing-library/react

Typescript

  1. Add TypeScript:

    • npm install typescript @types/node @types/react @types/react-dom
    • yarn add typescript @types/node @types/react @types/react-dom
    • pnpm add typescript @types/node @types/react @types/react-dom
     
    { "scripts": { "type-check": "tsc --noEmit" } }

Running Scripts

  1. Run any npm script:
    • npm run <script-name>
    • yarn <script-name>
    • pnpm <script-name>

By following this cheat sheet, you can effectively manage and run your React Vite projects.

Author
No Image
Admin
MmantraTech

Mmantra Tech is a online platform that provides knowledge (in the form of blog and articles) into a wide range of subjects .

You May Also Like

Write a Response