如何启动react文件
启动 React 项目的方法
前提条件
确保已安装 Node.js(建议使用 LTS 版本)和 npm/yarn。可通过以下命令检查版本:
node -v
npm -v
方法一:使用 Create React App(官方脚手架)
-
全局安装 Create React App(若已安装可跳过)
npm install -g create-react-app -
创建新项目
npx create-react-app my-app或使用 yarn:
yarn create react-app my-app -
进入项目目录并启动
cd my-app npm start默认在浏览器打开
http://localhost:3000。
方法二:使用 Vite(快速构建工具)
-
通过 Vite 创建 React 项目
npm create vite@latest my-react-app --template react -
安装依赖并启动
cd my-react-app npm install npm run dev开发服务器通常运行在
http://localhost:5173。
方法三:已有项目的启动
若项目已存在,直接安装依赖并运行:
npm install
npm start
或使用 yarn:
yarn install
yarn start
常见问题处理
-
端口冲突:修改启动端口
在package.json的scripts中添加:"start": "set PORT=3001 && react-scripts start" -
依赖错误:清除缓存并重装

npm cache clean --force rm -rf node_modules npm install






