How to Set Up a New Vite Application
Vite is a modern build tool that offers a fast development experience for modern web projects. It is particularly well-suited for React applications, providing an incredibly fast development server and efficient hot module replacement. This guide will walk you through how to set up a new React application using Vite.
Prerequisites
Before you start, make sure you have the following installed on your computer:
- Node.js (version 14 or later recommended): You can download it from Node.js official website.
- npm (Node Package Manager) or yarn: These are package managers used for installing dependencies.
To verify if Node.js and npm are installed, run the following commands:
If installed correctly, the versions will be displayed in your terminal.
Step 1: Create a New Vite Application
To create a new React application using Vite, run the following command in your terminal:
Let's break down each part of the command:
npm create
: This is used to initialize a new project by using an npm initializer package. In this case, it uses Vite's initializer.vite@latest
: Specifies that we want to use the latest version of Vite for creating the application.my-vite-app
: This is the name of your project. You can replacemy-vite-app
with any name you prefer for your project directory.
Alternatively, you can use yarn:
After running the command, you'll be prompted to select a framework. Choose React from the list, and you will also be asked if you want to use JavaScript or TypeScript. Here’s an explanation of the prompts:
- Framework Selection: This is where you choose the framework you'd like to use. In this guide, we're using React.
- JavaScript or TypeScript: You can select either JavaScript or TypeScript. JavaScript is the standard choice, but TypeScript provides static typing, which can help catch errors earlier in the development process.
Step 2: Navigate into Your Project Directory
Once the setup is complete, navigate into your new project directory:
cd
: This command stands for "change directory." It allows you to move into your newly created project folder (my-vite-app
).
Step 3: Install Dependencies
Now that you have your Vite project structure in place, install the necessary dependencies by running:
or, if you're using yarn:
npm install
: This command installs all of the dependencies specified in thepackage.json
file. These dependencies are necessary for your application to run.yarn
: This is the equivalent command if you are using Yarn as your package manager.
Step 4: Run the Development Server
To start your Vite development server, use the following command:
or, if you're using yarn:
npm run dev
: This command starts the Vite development server, allowing you to see your application in action. Vite’s development server is known for its lightning-fast startup times and efficient hot module replacement (HMR).
By default, the development server will be running at http://localhost:5173. Open this URL in your browser to see your new React application up and running.
Step 5: Understanding the Project Structure
After creating your Vite project, you'll see several files and folders. Here's a quick overview of the key parts:
index.html
: The entry point of your application, where the root element is defined.src/
: This directory contains your React components and the main application logic. By default, there is amain.jsx
file where your app is initialized, and anApp.jsx
file containing the main component.node_modules/
: This folder contains all the installed dependencies.package.json
: A file that lists dependencies and scripts related to your project. It also includes metadata such as the project name and version.
Step 6: Modify the Default Application
To make changes to your new Vite application, open src/App.jsx
in your favorite code editor and modify it to display a custom message:
Save the file, and your browser should automatically refresh to reflect the updated content.
Step 7: Adding a New Component
To add a new component in your Vite project, follow these steps:
- Inside the
src/
folder, create a new file calledHello.jsx
. - Add the following code to define your
Hello
component:
- Now, import and use this component inside
App.jsx
:
Save the changes, and you will see the Hello
component rendered below your welcome message.
Step 8: Adding Styles
Vite supports both CSS and CSS modules. To add styles to your project, you can modify the existing App.css
file located in the src/
directory:
Feel free to add new styles or create additional CSS files as needed to style your components.
Step 9: Building for Production
When you are ready to deploy your Vite application, you can create an optimized production build by running the following command:
or, if you're using yarn:
npm run build
: This command generates a production-ready build of your application. It creates adist/
directory containing all of the optimized assets.
Conclusion
Setting up a new Vite application is fast and straightforward. Vite's modern tooling provides a smooth development experience with lightning-fast HMR (Hot Module Replacement). With your Vite project up and running, you can start building, add new components, and create a dynamic UI. Vite is ideal for modern JavaScript development, providing the speed and flexibility needed for both small and large projects.
Happy coding!