How to Install React: Step-by-Step Guide for Beginners
To install
React, first ensure you have Node.js installed. Then run npm install react react-dom or yarn add react react-dom in your project folder to add React libraries.Syntax
Use the package manager commands to add React to your project:
- npm:
npm install react react-dom - yarn:
yarn add react react-dom
react is the core library for building UI components, and react-dom helps React work with the web browser.
bash
npm install react react-dom
Example
This example shows how to create a new React project using create-react-app, which installs React automatically and sets up everything you need.
bash
npx create-react-app my-app cd my-app npm start
Output
Starting the development server...
Compiled successfully!
You can now view my-app in the browser.
Common Pitfalls
Common mistakes when installing React include:
- Not having
Node.jsinstalled before running install commands. - Running install commands outside your project folder.
- Forgetting to install both
reactandreact-dom. - Using outdated package managers or versions.
Always check your package.json to confirm React is listed as a dependency.
bash
npm install react # Missing react-dom causes errors when rendering # Correct way: npm install react react-dom
Quick Reference
Summary tips for installing React:
- Install
Node.jsfirst from nodejs.org. - Use
npm install react react-domoryarn add react react-dom. - Consider using
create-react-appfor a ready setup. - Run install commands inside your project folder.
Key Takeaways
Install Node.js before adding React to your project.
Use npm or yarn to install both react and react-dom packages.
Run install commands inside your project folder to avoid errors.
Create React apps easily with create-react-app tool.
Check package.json to confirm React is installed correctly.