React installation process
Install Node.js and npm
React requires Node.js and npm (Node Package Manager). Download and install the latest stable version of Node.js from https://nodejs.org/. npm is included with Node.js.
To verify the installation, run the following commands in your terminal:
#bash
node -v
npm -v
Set Up a React Application
We can create a React app using the create-react-app tool or other modern tools like Vite. Here's how to use create-react-app:
#bash
npx create-react-app my-app
Replace my-app with your desired project name. The npx command ensures you use the latest version of create-react-app.
Navigate to your project directory and start the server:
#bash
# Navigate to project directory
cd my-app
#Start the React server
npm start
This will open your app in the browser at
http://localhost:3000.
Key Differences Between npx and npm:
Feature | npm | npx |
|---|---|---|
Purpose | Installs and manages packages. | Executes packages without installing them globally. |
Global Installation | Requires global installation for CLI tools. | No need for global installation; runs directly. |
Version Management | May use outdated globally installed versions. | Always uses the latest version of the package. |
Use Case | Installing dependencies for projects. | Running one-time commands or tools. |
Example | npm install -g create-react-app | npx create-react-app my-app |
When to Use npx vs npm
- Use
npmwhen:- You need to install a package as a dependency for your project.
- You want to install a package globally for frequent use.
- Use
npxwhen:- You want to run a package or CLI tool without installing it globally.
- You need to ensure you're using the latest version of a package.
