React / 3 MIN READ
Create Vite (Same Dir)
Setting up Vite in the same directory
From the original Fervor library. Examples may use older package versions.
Creating a React application with Vite directly in the current directory streamlines the setup process, especially when you’re integrating into an existing project structure or simply prefer not to create a new folder. This tutorial will guide you through initializing a React application using Vite in the current directory, covering the setup steps, basic configuration, and a brief example to kickstart your development.
Prerequisites
Ensure the following are installed on your system:
- Node.js (version 12 or later)
- npm (version 6 or later) or Yarn
Verify your Node.js and npm installations by running node -v and npm -v in your terminal.
Step 1: Initializing Your React App with Vite
To create a React app with Vite in the current directory, follow these steps:
-
Open your terminal and navigate to the directory where you want to create your app.
-
Run the Vite initialization command with a slight modification to create the project in the current directory:
npm create vite@latest . -- --template reactThis command tells Vite to scaffold a React project in the current directory (
.). The extra--is necessary to pass the--template reactargument through npm to thecreate-vitescript. -
Install the dependencies:
npm installThis step downloads all the necessary packages required to run and build your React application.
Step 2: Understanding the Project Structure
After the setup, your current directory will contain several important files and directories:
index.html- The entry HTML file where your React app gets loaded.src/main.jsx- The main JavaScript entry point for your React application.src/App.jsx- A sample React component provided by Vite.vite.config.js- Configuration file for Vite, allowing you to customize the build and development settings.
Step 3: Running Your React App
-
Start the development server by executing:
npm run dev -
View your app by opening
http://localhost:3000in your browser. You should see your React app running.
Step 4: Modifying Your Application
-
Open
src/App.jsxin your favorite text editor. -
Change the content within the
<div>to personalize your app, for instance:function App() { return ( <div className="App"> <h1>Welcome to Vite and React!</h1> </div> ); } export default App; -
Save the file. The development server automatically updates the browser with your changes.
Step 5: Building and Previewing for Production
When you’re ready to deploy your application:
-
Build your application for production by running:
npm run buildThis command generates a
distdirectory containing optimized assets for your app. -
Preview the production build locally with:
npm run serveThis serves your built application on a local server, allowing you to preview the production version before deployment.
Conclusion
You’ve successfully set up a React application using Vite in your current directory. This method is particularly useful for quickly bootstrapping projects in an existing workspace or when you prefer to avoid creating extra directories. Vite offers a modern and fast development experience, focusing on performance and simplicity. From here, you can proceed to expand your application, explore Vite’s extensive plugin ecosystem, and customize your build configuration as needed.
Happy coding!