Running a React Application
Table of Contents + β
In the last lesson, Understanding React Project Structure, you saw what every file in a new React project is for. But a project sitting on your disk does nothing on its own, so now letβs run it and watch your changes show up in the browser.
π€ Why You Need to Run the App
Here is the problem you hit right away:
- You wrote some code in
src/App.jsx, but a.jsxfile is just text on your computer. - A browser cannot read that file directly, so there is no web page to look at yet.
The fix is a small program called a development server. Here is what it does for you:
- It takes your React code and turns it into something a browser understands.
- It serves that as a live web page on your own computer.
- You start it once, and it keeps running while you work.
When you created the project, the setup tool Vite gave you a few ready-made commands. Letβs go through the ones you will use every single day.
π Starting the Development Server
The command to start working is npm run dev. Run it from inside your project folder.
npm run devThis starts the development server. After a second or two, you will see something like this in your terminal.
Output
VITE v5.4.0 ready in 412 ms
β Local: http://localhost:5173/ β Network: use --host to expose β press h + enter to show helpThe important line is Local: http://localhost:5173/. That is the address of your running app. Letβs break down what that address means:
localhostmeans this computer. So the app is running on your own machine, not on the internet, and nobody else can see it yet.5173is the port. A port is just a numbered door on your computer that a program listens on, and Vite uses door number 5173 by default.
So open your browser and go to http://localhost:5173. You will see the starter React page. That is your app, live.
Tip
The terminal that is running npm run dev has to stay open. That window is your server. If you close it or stop it, the page at localhost:5173 stops working. To stop the server on purpose, click the terminal and press Ctrl + C.
π₯ Seeing Your Changes Instantly with Fast Refresh
Now the best part. With the server still running, letβs change the app and watch what happens.
Open src/App.jsx in your editor. Find the heading text and change it.
function App() { return ( <div> <h1>Hello from my first React app!</h1> </div> );}
export default App;Save the file, then look at your browser:
- You did not refresh the page, and you did not even touch the browser.
- But the heading already changed to your new text.
That is Fast Refresh. Here is the idea:
- It is Reactβs feature that watches your files for changes.
- The moment you save one, it updates only the part of the page that changed.
- The rest of the page stays exactly as it was.
Why does that matter? The old way meant saving a file, switching to the browser, and hitting refresh every single time. Fast Refresh removes all of that:
- You save, and the screen updates in a fraction of a second.
- It keeps your appβs current state where it can. So if you typed text into a box, that text often stays after the update.
- You stay in the editor and just keep coding, with no clicking back and forth.
Note
Fast Refresh is sometimes called hot reload. They mean the same idea here. The page updates itself while you work, without a full reload of the whole page.
π¦ Building for Production
npm run dev is for you, while you build the app:
- It is fast to update but not optimized.
- It only runs on your machine.
So when the app is finished and you want to put it on the real internet for other people, you need a different command. That command is npm run build.
npm run buildHere is what this command does:
- It takes all your code and prepares an optimized version that loads fast for real users.
- It does work like shrinking the files and removing anything unused.
When it finishes, you will see output like this.
Output
vite v5.4.0 building for production...β 34 modules transformed.dist/index.html 0.46 kBdist/assets/index-a1b2c3d4.css 1.39 kBdist/assets/index-e5f6g7h8.js 143.21 kBβ built in 1.84sNotice the new dist folder. Here is what it is:
- The
distfolder (short for distribution) is the finished, ready-to-ship version of your app. - These are the files you upload to a hosting service like Netlify or Vercel to put your site online.
π Previewing the Production Build
There is one more command that pairs with build. After you run npm run build, you can test that finished version on your own machine first with npm run preview.
npm run previewThis starts a small server that serves the real files from your dist folder, just like a hosting service would.
Output
β Local: http://localhost:4173/ β Network: use --host to exposeOpen that address and here is what you get:
- You are looking at the exact build your users will get.
- It is a good habit to preview the build before you ship it, just to be sure the optimized version still works the way you expect.
Here is a quick map of the commands so you can keep them straight.
| Command | What it does | When you use it |
|---|---|---|
npm run dev | Starts the development server with Fast Refresh | Every day, while you are coding |
npm run build | Creates the optimized dist folder | When the app is ready to go live |
npm run preview | Serves the built dist folder locally | To test the build before shipping |
πͺ The Everyday Workflow
Put together, here is what running and editing a React app looks like in practice.
-
Open the project folder
Open a terminal and make sure you are inside your project folder, the one that has
package.jsonin it. -
Start the dev server
Run the command that starts everything.
Terminal window npm run dev -
Open the app in your browser
Go to the address from the terminal. It is usually this one.
http://localhost:5173 -
Edit and save
Change something in
src/App.jsx, then save the file. Watch the browser update on its own through Fast Refresh. Keep doing this as you build. -
Stop when you are done
Click the terminal and press
Ctrl + Cto stop the server.
β οΈ Common Mistakes
A few things trip people up the first time they run an app.
- Closing the terminal and wondering why the page broke. The terminal running
npm run devis the server. Close it and the page atlocalhost:5173stops responding. Leave it open while you work. - Running the command in the wrong folder.
npm run devonly works inside your project folder, the one withpackage.json. If you run it somewhere else, you get an error. Move into the right folder first. - Refreshing the browser by habit. You do not need to. Fast Refresh already updated the page when you saved. A manual refresh is harmless, but it is wasted effort.
- Thinking
npm run buildis for daily coding. It is not. Thedistfolder does not update when you save a file. Usenpm run devwhile building, andnpm run buildonly when you are ready to ship.
β Running in the wrong place:
# You are in your home folder, not the projectnpm run dev# npm errors here β there is no project in this folderβ Move into the project first, then run it:
cd my-react-appnpm run devβ Best Practices
- Keep the
npm run devterminal open in one window and your editor in another. Save in the editor, glance at the browser. That loop is the whole rhythm of React work. - Bookmark
http://localhost:5173so you can jump back to your running app fast. - If
localhost:5173ever looks empty or stuck, check the terminal first. Errors in your code show up there in plain text. - Run
npm run build, thennpm run preview, before you put a site online. Test the real version, not just the dev version.
π§© What Youβve Learned
A quick recap of what you can now do.
- β
Start a React app with
npm run devand open it athttp://localhost:5173. - β
Understand that
localhostmeans your own computer and5173is the port the server listens on. - β
Edit
src/App.jsx, save, and see the change instantly thanks to Fast Refresh, with no manual page refresh. - β
Create an optimized production version with
npm run build, which makes thedistfolder. - β
Test that production build locally with
npm run previewbefore shipping.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
Which command starts the development server so you can see your app in the browser?
Why: npm run dev starts the development server, usually on http://localhost:5173, and keeps it running while you code.
- 2
What does Fast Refresh do when you save a file in src/App.jsx?
Why: Fast Refresh watches your files and updates only the part that changed the moment you save, so you do not refresh the browser yourself.
- 3
What does npm run build create?
Why: npm run build prepares an optimized version of your app and places the ready-to-ship files in the dist folder.
- 4
In the address http://localhost:5173, what is 5173?
Why: 5173 is the port, a numbered door on your computer that the dev server listens on; localhost means your own machine.
π Whatβs Next?
You can now run your app and watch your edits appear live. That means it is finally time to learn the language you will write inside those components, the special syntax that makes React feel like writing HTML in JavaScript.