Creating a React Project with Vite
Learn how to create a React application using Vite, install its dependencies, understand the basic commands, and start the project.
1. What Is Vite?
Vite is a frontend development tool used to create and run modern web applications. It provides a development server and tools for building applications for production.
We can use Vite to create a React project without manually configuring every development tool.
2. React Project Creation Flow
Follow these steps to create and run your first Vite-powered React application.
Open Terminal
Use Command Prompt, PowerShell, or VS Code terminal.
Create Vite Project
Generate a React project from the command line.
Install Dependencies
Install the packages specified by the project.
Run Development Server
Start the application using the npm script.
React Application Ready
Open the local URL in your browser.
3. Before Creating the Project
Make sure the following items are ready:
Node.js
Installed on your computer with a version supported by the current Vite release.
npm
Available in your terminal for installing project packages.
Terminal
Open Command Prompt, PowerShell, or the VS Code terminal.
Code Editor
Visual Studio Code or another editor for editing project files.
node --version and npm --version
before continuing. If the current Vite release requires a newer
Node.js version, update Node.js before creating the project.
4. Step 1: Open the Terminal
First, decide where you want to save your React project. Open a terminal and navigate to that location.
Example: Navigate to the Desktop
cd Desktop
This command moves into the Desktop folder when it is available relative to your current terminal location.
5. Step 2: Create a React Project Using Vite
Run the following command to create a project named
my-react-app using Vite's React template.
npm create vite@latest my-react-app -- --template react
Understand the Command
npm create vite@latest
Runs the Vite project creation tool.
my-react-app
Sets the name of your new project folder.
-- --template react
Passes the React template option to the Vite scaffolding tool.
6. Step 3: Enter the Project Folder
After the project has been created successfully, move into the project directory.
cd my-react-app
This command makes the project folder the current working directory. The next commands should be run from inside it.
my-react-app\
7. Step 4: Install Project Dependencies
A newly generated project contains a
package.json file. This file describes the
dependencies and available npm scripts for the application.
Run the following command inside the project folder:
npm install
What Does npm install Do?
Reads package.json
Checks the project's dependency definitions.
Downloads Packages
Retrieves the dependencies required by the project.
Creates node_modules
Stores installed project packages in the dependency directory.
Creates or Updates Lockfile
Uses npm's lockfile to record resolved dependency versions.
8. Step 5: Run the React Application
After installing the dependencies, use the project's development script to start the development server.
npm run dev
Vite will display a local URL in the terminal. The default address commonly uses port 5173, but the actual URL may differ if another port is needed.
Open the exact URL printed in your terminal.
Ctrl + C.
9. All Commands Together
Here is the complete sequence for creating and running the React project from the terminal.
npm create vite@latest my-react-app -- --template react
cd my-react-app
npm install
npm run dev
10. Preview of the Project Structure
Vite generates a starting set of files for the selected template. A React project commonly contains a structure similar to this:
src/App.jsx
Contains the main application component.
src/main.jsx
Connects the React application to the HTML root element.
package.json
Contains project metadata, dependencies, and npm scripts.
node_modules
Contains the packages installed for the project.
The exact files may vary depending on the Vite version and template used. We will explore the project structure in the next lesson.
11. Try a Simple React Component
After the application is running, open
src/App.jsx in your editor. Replace its content
with the following small example.
function App() {
return (
<div>
<h1>Welcome to React!</h1>
<p>My first React project is running.</p>
</div>
);
}
export default App;
Expected Browser Output
Welcome to React!
My first React project is running.
12. Common Errors and Solutions
| Problem | Possible Reason | Solution |
|---|---|---|
| Node.js version error | Installed Node.js does not meet the Vite requirements. | Install a supported Node.js version and reopen the terminal. |
| Project creation fails | Network, npm, or command-line issue. | Check the exact terminal error and try again. |
npm install fails |
Network or package dependency issue. | Check internet access and the error details. |
Missing script: dev |
The current folder does not have the expected project script. | Check the current folder and the scripts in package.json. |
| Browser page does not load | The server may not be running or the URL may be incorrect. | Check the terminal and open the displayed local URL. |
| Port already in use | Another process uses the default port. | Use the alternative URL Vite displays in the terminal. |
Debugging Tip
Read the terminal message first. It often identifies whether the problem is related to Node.js, dependencies, project configuration, or the development server.
13. Practice Task
Task: Create Your First Vite React Project
- Open a new terminal.
- Create a project named
my-react-app. - Move into the project folder.
- Install the dependencies with
npm install. - Start the app using
npm run dev. - Open the local URL provided by Vite.
- Replace the content of
src/App.jsxwith the sample above. - Save the file and verify the output in your browser.
Summary
- Vite helps create and run a React project.
npm create vite@lateststarts project creation.cd my-react-appenters the project folder.npm installinstalls project dependencies.npm run devstarts the development server.src/App.jsxcontains the main component in this example.- Use the local URL shown in your terminal to open the application.