![]()
Setting up a React project using Create React App is a simple and efficient way to get started with React. Create React App is an officially supported tool that sets up a new React project with a sensible default configuration, so you don’t have to worry about configuring Webpack, Babel, or other build tools.
Here’s a step-by-step guide to setting up a new React project using Create React App:
1. Install Node.js and npm
Before you begin, ensure that you have Node.js and npm installed on your system. npm (Node Package Manager) comes with Node.js.
- You can download Node.js from here.
- To check if you have Node.js and npm installed, open your terminal (Command Prompt on Windows or Terminal on macOS/Linux) and type:
node -v
npm -v
If Node.js and npm are installed, you’ll see their version numbers.
2. Install Create React App Globally (Optional)
While you can directly use npx to create a React app without installing Create React App globally, you may want to install it globally if you plan to create many projects.
To install Create React App globally, run the following command:
npm install -g create-react-app
3. Create a New React Project
Now, you’re ready to create your React project. If you installed Create React App globally, you can run:
create-react-app my-app
If you prefer not to install it globally, you can use npx (which comes with npm 5.2+), so you don’t need to install Create React App separately:
npx create-react-app my-app
my-appis the name of your new project. You can replace it with whatever name you prefer.- This command will automatically create a new directory, install all necessary dependencies, and set up a basic React project structure.
4. Navigate to Your Project Directory
Once the project is created, navigate into the new project directory:
cd my-app
5. Start the Development Server
To start the React development server and launch your project in the browser, run:
npm start
This command will:
- Launch the development server.
- Open your default browser with the app running on
http://localhost:3000.
Any changes you make in the project will be automatically reflected in the browser without having to refresh it manually (thanks to Hot Module Replacement).
6. Project Structure
The default project structure created by Create React App looks like this:
my-app/
node_modules/
public/
index.html
src/
App.js
index.js
package.json
.gitignore
README.md
package-lock.json
public/index.html: The main HTML file that serves as a template. React components will be injected into this file.src/index.js: The entry point for the React app. This is where you’ll render theAppcomponent into the DOM.src/App.js: The main component for your app.package.json: Contains the project’s metadata and dependencies.node_modules/: The folder where all your dependencies are installed.
7. Install Additional Dependencies (Optional)
You may want to install additional dependencies depending on the features you need. For example:
- For routing (if you need multiple pages), you can install React Router:
npm install react-router-dom - For state management with Redux, you can install Redux and React-Redux:
npm install redux react-redux
8. Start Coding!
Now that your React app is set up, you can start editing the src/App.js file or create new components to build your app.
For example, you can change the App.js component to:
import React from 'react';
function App() {
return (
<div className="App">
<h1>Hello, React!</h1>
</div>
);
}
export default App;
Once you save the changes, the page will automatically update, and you’ll see your new content in the browser.
9. Building the App for Production
Once you are done building the app and are ready to deploy it, you can create an optimized production build by running:
npm run build
This will create a build/ directory containing a minified and optimized version of your app for production.
10. Deploying the App
You can deploy your React app to various platforms such as:
- Netlify
- Vercel
- GitHub Pages
- Heroku
Each platform has its own deployment process, but it generally involves pushing your code to a Git repository (GitHub, GitLab, etc.) and linking the repo to the deployment platform.
Summary
- Install Node.js and npm if you haven’t already.
- Use Create React App (either globally or with
npx) to create a new React project. - Navigate to your project directory and run
npm startto start the development server. - Begin coding in the
src/folder and build your app. - Optionally, install additional dependencies for routing, state management, etc.
- Use
npm run buildto create a production-ready version of your app.
