How to Run a React App Locally and Online
In this article
This guide walks you through running a React app in your browser for local development and then deploying it online so others can access it. You'll start with a local preview using npm start, then build the app and publish it to a free hosting service. No prior deployment experience is needed—just a basic understanding of the command line.
What You'll Need
- Node.js and npm installed (download from nodejs.org; npm comes with it)
- A code editor (VS Code, Sublime Text, or any text editor)
- A React app already created (if not, we'll create one in Step 1)
- A web browser (Chrome, Firefox, Edge, etc.)
- For online deployment: a free account on a hosting service like Netlify or Vercel (optional but recommended)
Step-by-Step
- Check Node.js and npm – Open your terminal (Command Prompt on Windows, Terminal on Mac/Linux). Run
node -vandnpm -v. If you see version numbers, you're ready. If not, install Node.js from the official website and restart your terminal. - Create a new React app (if you don't have one) – In your terminal, navigate to the folder where you want the app. Run
npx create-react-app my-app(replacemy-appwith your project name). This creates a new folder with all the React boilerplate. Wait for the installation to finish. - Navigate into the project folder – Run
cd my-app(or the name you used). This moves you into the project directory. - Start the local development server – Run
npm start. The terminal will show a message like Compiled successfully! and give you a local URL, usuallyhttp://localhost:3000. Your default browser should open automatically. If not, manually open that URL in your browser. - View your app in the browser – You'll see the default React welcome page with a spinning logo. Any changes you make to the source files (in the
srcfolder) will automatically reload the browser—this is called hot reloading. - Stop the local server when done – In the terminal, press
Ctrl+C(Windows/Linux) orCmd+C(Mac) to stop the server. - Build the app for production – When you're ready to go online, run
npm run build. This creates abuildfolder containing optimized, minified files ready for deployment. - Deploy online using Netlify (drag-and-drop method) – Go to app.netlify.com and sign up/log in. Click Add new site → Deploy manually. Drag and drop the
buildfolder onto the designated area. Netlify uploads it and gives you a live URL likehttps://random-name.netlify.app. That's it—your app is now online. - Deploy online using Vercel (alternative) – Install Vercel CLI with
npm install -g vercel. In your project folder, runvercel. Follow the prompts (login, link to project, etc.). Vercel detects React and builds automatically, then provides a live URL. You can also use the Vercel dashboard to import your project from GitHub. - Update your online app – If you make changes, rebuild with
npm run buildand re-upload the newbuildfolder (Netlify) or runvercel --prod(Vercel).
Common Mistakes
- Forgetting to run
npm install– If you cloned a project from GitHub, you must runnpm installfirst to install dependencies. Skipping this causes errors like Cannot find module 'react'. - Using the wrong terminal folder – Running
npm startfrom the parent folder (not the project root) will fail. Always ensure you're inside the project folder wherepackage.jsonexists. - Port already in use – If port 3000 is busy, React will ask if you want to use another port. Press
yto use 3001, or close the other process. - Deploying the source folder instead of
build– On Netlify, dragging the whole project folder (withsrc,node_modules) won't work. You must drag thebuildfolder only. - Not rebuilding after changes – If you edit code and then upload the old
buildfolder, your changes won't appear online. Always rebuild first.
Tips
- Use
npm startfor development,npm run buildfor production – The dev server is not optimized for public hosting; the build is. - Set up a Git repository – Initialize Git in your project (
git init) and commit changes. This makes it easier to deploy via GitHub integration on Netlify/Vercel and roll back if needed. - Customize the title and metadata – Edit
public/index.htmlto change the browser tab title and meta description. - Use environment variables for secrets – Never hardcode API keys. Create a
.envfile (e.g.,REACT_APP_API_KEY=...) and access viaprocess.env.REACT_APP_API_KEY. - Test the production build locally – Run
npx serve -s buildto preview the built app locally before deploying. This catches issues like broken asset paths.
Troubleshooting
- Error: 'react-scripts' is not recognized – This means dependencies aren't installed. Run
npm installand try again. - Blank page after deployment – Check the browser console for errors. If you used a custom
homepageinpackage.json, it might be causing incorrect asset paths. Remove it or set it to your domain. - Local server starts but browser shows 'This site can't be reached' – Check if the terminal shows a different port (e.g., 3001). Also, try accessing
http://127.0.0.1:3000instead oflocalhost. - Netlify deploy fails – Make sure you're uploading the
buildfolder, not the project root. If you dragged the right folder and it still fails, try a different browser or clear cache. - Changes not reflecting in browser – If hot reload isn't working, stop the server (
Ctrl+C) and restartnpm start. Also check that you're editing files insidesrc, notpublic(except for index.html).
Frequently Asked Questions
Do I need to pay to host a React app online?
No. Services like Netlify and Vercel offer free tiers that are perfect for personal projects and portfolios. They provide a subdomain (e.g., your-app.netlify.app) and handle HTTPS automatically.
Can I run a React app without installing Node.js?
No, Node.js is required for the development server and build process. However, you can use online code sandboxes like CodeSandbox or StackBlitz to run React entirely in the browser, but that's not the same as running it locally.
What is the difference between npm start and npm run build?
npm start runs a development server with hot reloading, optimized for coding. npm run build creates a production-ready bundle with minified code, optimized for performance, which is what you deploy online.
How do I update my app after deploying?
Make changes to your code, run npm run build again, and re-upload the new build folder to Netlify (or run vercel --prod for Vercel). Your live URL will update automatically.
Can I use a custom domain with my React app?
Yes, both Netlify and Vercel allow you to add a custom domain (e.g., www.yourdomain.com) in their dashboard. You'll need to update your domain's DNS settings to point to their servers.
Why does my app look different in the browser than in the code?
This often happens due to caching. Hard refresh your browser with Ctrl+F5 (Windows/Linux) or Cmd+Shift+R (Mac). If the issue persists, check the console for errors and ensure you're viewing the correct file.
Running a React app locally is straightforward with npm start, and deploying online is just a matter of building and uploading the build folder. With the steps above, you can preview your app in the browser and share it with the world in minutes.