如何配置react环境
安装 Node.js 和 npm
确保系统已安装 Node.js(包含 npm)。可通过官网下载最新版本,或使用版本管理工具如 nvm 管理多个版本。安装后验证版本:
node -v
npm -v
创建 React 项目
使用官方工具 create-react-app 快速初始化项目:
npx create-react-app my-app
cd my-app
或通过 Vite 创建(更轻量):
npm create vite@latest my-app --template react
cd my-app
安装必要依赖
根据项目需求安装常用库:
npm install react-router-dom axios @reduxjs/toolkit
开发依赖如 ESLint、Prettier 可按需添加:
npm install eslint prettier --save-dev
配置开发环境
修改 package.json 添加自定义脚本:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test"
}
通过 .env 文件配置环境变量:
REACT_APP_API_URL=https://api.example.com
启动开发服务器
运行以下命令启动本地开发:
npm start
项目默认在 http://localhost:3000 运行,代码修改后会热更新。







