Setting up a React project with Vite is quite straightforward due to Vite's efficient development server and fast build times. Here's a step-by-step guide on how to set up a basic React project using Vite:
Prerequisites:Ensure you have Node.js installed on your machine.
Steps:-
Create a New React Project:
You can use the
create-vitepackage to scaffold a new React project:npm init vite@latest my-react-app --template react # Or with Yarn yarn create vite my-react-app --template reactReplace
my-react-appwith the name of your project. -
Navigate to Your Project Directory:
cd my-react-app -
Install Dependencies:
npm install # Or with Yarn yarn -
Run the Development Server:
npm run dev # Or with Yarn yarn devThis will start the Vite development server. By default, it will run on http://localhost:3000.
-
Start Building Your React App:
You can begin developing your React application within the src directory. Vite's development server supports fast HMR (Hot Module Replacement) for efficient development.
-
Build for Production:
When you're ready to deploy your React app, use the following command to create a production build.
npm run build # Or with Yarn yarn buildThis command generates a production-optimized build in the
distdirectory.
- Vite uses ES modules by default, allowing for faster development and improved performance during development.
- You can import ES modules directly in your code without additional configuration.
- Vite supports TypeScript, SCSS, and other preprocessors out of the box. You can modify the project structure as needed.
This setup provides a simple and efficient development environment for building React applications using Vite, taking advantage of its speed and optimized development experience.