服务公告
React - 安装配置 配置详解
发布时间:2026-05-03 18:01
一、前言
干过前端的人都知道,新机器上配React环境是最烦的——Node版本不对、npm源太慢、create-react-app卡在半空,各种坑轮着踩一遍。这次把10年踩坑经验整理出来,照着做10分钟跑起来。
二、操作步骤
步骤1:检查并安装Node.js环境
React 18要求Node 14.18以上,先看看你机器上有没有Node。
$ node --version v16.19.0 $ npm --version 8.19.3
如果显示command not found,说明没装。继续往下看。
步骤2:通过nvm管理Node版本(推荐)
搞前端的都知道,用nvm管理Node版本比直接装强太多了,切换版本不打架。CentOS/RHEL和Ubuntu通用安装方式:
# Ubuntu/Debian $ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash $ source ~/.bashrc # CentOS/RHEL(需要先装git和curl) $ yum install -y git curl $ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash $ source ~/.bashrc
安装完成后验证nvm是否正常工作:
$ command -v nvm nvm $ nvm --version 0.39.3
步骤3:安装Node LTS版本
React官方推荐LTS版本,稳定性好。直接用nvm安装:
$ nvm install --lts Installing node v18.17.0 (with npm 9.6.7)... $ nvm use --lts Now using node v18.17.0 (npm 9.6.7) $ node --version v18.17.0
步骤4:配置npm国内镜像源
npm默认源在海外,创建React项目时下载依赖慢到怀疑人生。换成淘宝镜像速度快好几倍:
$ npm config set registry https://registry.npmmirror.com $ npm config get registry https://registry.npmmirror.com