Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Run a React App Locally and Online

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Check Node.js and npm – Open your terminal (Command Prompt on Windows, Terminal on Mac/Linux). Run node -v and npm -v. If you see version numbers, you're ready. If not, install Node.js from the official website and restart your terminal.
  2. 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 (replace my-app with your project name). This creates a new folder with all the React boilerplate. Wait for the installation to finish.
  3. Navigate into the project folder – Run cd my-app (or the name you used). This moves you into the project directory.
  4. Start the local development server – Run npm start. The terminal will show a message like Compiled successfully! and give you a local URL, usually http://localhost:3000. Your default browser should open automatically. If not, manually open that URL in your browser.
  5. 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 src folder) will automatically reload the browser—this is called hot reloading.
  6. Stop the local server when done – In the terminal, press Ctrl+C (Windows/Linux) or Cmd+C (Mac) to stop the server.
  7. Build the app for production – When you're ready to go online, run npm run build. This creates a build folder containing optimized, minified files ready for deployment.
  8. 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 build folder onto the designated area. Netlify uploads it and gives you a live URL like https://random-name.netlify.app. That's it—your app is now online.
  9. Deploy online using Vercel (alternative) – Install Vercel CLI with npm install -g vercel. In your project folder, run vercel. 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.
  10. Update your online app – If you make changes, rebuild with npm run build and re-upload the new build folder (Netlify) or run vercel --prod (Vercel).

Common Mistakes

  • Forgetting to run npm install – If you cloned a project from GitHub, you must run npm install first to install dependencies. Skipping this causes errors like Cannot find module 'react'.
  • Using the wrong terminal folder – Running npm start from the parent folder (not the project root) will fail. Always ensure you're inside the project folder where package.json exists.
  • Port already in use – If port 3000 is busy, React will ask if you want to use another port. Press y to use 3001, or close the other process.
  • Deploying the source folder instead of build – On Netlify, dragging the whole project folder (with src, node_modules) won't work. You must drag the build folder only.
  • Not rebuilding after changes – If you edit code and then upload the old build folder, your changes won't appear online. Always rebuild first.

Tips

  • Use npm start for development, npm run build for 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.html to change the browser tab title and meta description.
  • Use environment variables for secrets – Never hardcode API keys. Create a .env file (e.g., REACT_APP_API_KEY=...) and access via process.env.REACT_APP_API_KEY.
  • Test the production build locally – Run npx serve -s build to 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 install and try again.
  • Blank page after deployment – Check the browser console for errors. If you used a custom homepage in package.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:3000 instead of localhost.
  • Netlify deploy fails – Make sure you're uploading the build folder, 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 restart npm start. Also check that you're editing files inside src, not public (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.