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-vite
package 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 react
Replace
my-react-app
with 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 dev
This 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 build
This command generates a production-optimized build in the
dist
directory.
- 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.