Before attempting to install React, ensure that Node.js is properly installed on you're system. You can verify the installation by running node -v and npm -v in your terminal.
Resolving Permission Errors
When installing React using the standard command prompt, you might encounter permission-related errors. A typical installation command is:
npm install -g create-react-app
If the installation fails with an error message indicating insufficient permissions, such as:
npm ERR! the command again as root/Administrator.
This indicates that the current terminal session lacks the necessary administrative privileges to write to global npm directories.
Solution: Search for "Command Prompt" or "PowerShell" in the Windows Start menu, right-click, and select "Run as administrator." Execute the installation command again within this elevated session.
Accelerating Download Speeds
The default npm registry (https://registry.npmjs.org/) is hosted overseas and can result in slow download speeds for users in certain regions.
Checking the Current Registry
Run the following command to view your current npm registry configuration:
npm config get registry
Switching to a Mirror Registry
To improve download speeds, switch to a domestic mirror. For instance, the Huawei Cloud mirror is a reliable alternative:
npm config set registry https://mirrors.huaweicloud.com/repository/npm/
After setting the new registry, verify the change by running npm config get registry again. You can now proceed with the installation:
npm install -g create-react-app
Note: If you encounter certificate validation errors or the download stalls, press Ctrl + C to terminate the process and try a different mirror source.
Creating a New React Project
Once the CLI tool is installed, navigate to the directory where you wish to create your project. Use the cd command to change directories:
d:
cd D:\Projects\WebDevelopment
Initialize a new React application using the following command syntax:
npx create-react-app my-application
The process will take a few minutes to install dependencies. Upon successful completion, you will see a success message along with available commands:
Success! Created my-application at D:\Projects\WebDevelopment\my-application
Inside that directory, you can run several commands:
npm start
Starts the development server.
npm run build
Bundles the app into static files for production.
npm test
Starts the test runner.
Runninng the Development Server
Navigate into the newly created project folder and start the local development server:
cd my-application
npm start
This will launch the React application in your default web browser, typically accessible at http://localhost:3000.
Recommended Resources
For further development, consider exploring the following React ecosystem libraries:
- Ant Design: A comprehensive enterprise-grade UI design language and React component library.
- UmiJS: A plugin-based enterprise-level frontend application framework.
- DvaJS: A lightweight front-end framework based on Redux and Redux-saga.