一、開發(fā)環(huán)境搭建
1含友、更新brew
命令:
brew update
解釋:
更新brew的所有本地包
2替裆、安裝/更新node
命令:
brew install node
brew upgrade node
解釋:
安裝node
更新node
3校辩、安裝cnpm
命令:
npm install -g cnpm --registry=https://registry.npm.taobao.org
解釋:
npm install[安裝命令] -g[全局安裝參數(shù)] cnpm[cnpm包名稱] --registry[指定cnpm軟件包源] = https://registry.npm.taobao.org[cnpm軟件包源地址]
4、安裝webpack
命令
cnpm install webpack
解釋:
cnpm install[安裝命令] webpack[軟件包名]
5辆童、安裝create-react-app
命令:
cnpm install create-react-app
解釋:
cnpm install[安裝命令] create-react-app[軟件包名]
6宜咒、安裝babel-loader
命令:
cnpm install babel-loader@8.0.4
解釋:
cnpm install[安裝命令] babel-loader@8.0.4 [軟件包名@版本號](版本號為可選參數(shù),不添加則自動下載最新版本)
7把鉴、安裝react
命令:
cnpm install react
解釋:
cnpm install [安裝命令] react[軟件包名]
8故黑、安裝react-dom
命令:
cnpm install react-dom
解釋:
cnpm install[安裝命令] react-dom[軟件包名]
二、創(chuàng)建第一個react工程
1庭砍、創(chuàng)建基礎(chǔ)工程
命令:
create-react-app helloworld
解釋:
使用create-react-app創(chuàng)建一個名為helloworld的工程目錄
2场晶、拿出一包咖啡泡上
命令:
別放糖,少放奶
解釋:
解釋怠缸?解釋什么诗轻?會下載很多東西,喝杯咖啡等著就行了
3揭北、沒了
三扳炬、運(yùn)行helloworld
1、進(jìn)入helloworld工程目錄搔体,啟動工程
命令:
cd helloworld
npm start
解釋:
進(jìn)入helloworld工程目錄
啟動工程
2恨樟、查看運(yùn)行效果
瀏覽器輸入http://localhost:3000
若正常啟動瀏覽器將顯示一個react頁面
四、坑
1疚俱、Q:npm start執(zhí)行失敗劝术,日志如下
[@Freedoms:helloworld (master)]$ npm start
> helloworld@0.1.0 start /Users/freedoms/work/react/helloworld
> react-scripts start
There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.
The react-scripts package provided by Create React App requires a dependency:
"babel-loader": "8.0.4"
Don't try to install it manually: your package manager does it automatically.
However, a different version of babel-loader was detected higher up in the tree:
/Users/freedoms/work/react/helloworld/node_modules/babel-loader (version: 8.0.5)
Manually installing incompatible versions is known to cause hard-to-debug issues.
If you would prefer to ignore this check, add
SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.
To fix the dependency tree, try following the steps below in the exact order:
1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
2. Delete node_modules in your project folder.
3. Remove "babel-loader" from dependencies and/or devDependencies in the package.json file in your project folder.
4. Run npm install or yarn, depending on the package manager you use.
In most cases, this should be enough to fix the problem.
If this has not helped, there are a few other things you can try:
5. If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
This may help because npm has known issues with package hoisting which may get resolved in future versions.
6. Check if /Users/freedoms/work/react/helloworld/node_modules/babel-loader is outside your project directory.
For example, you might have accidentally installed something in your home folder.
7. Try running npm ls babel-loader in your project folder.
This will tell you which other package (apart from the expected react-scripts) installed babel-loader.
If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That would permanently disable this preflight check in case you want to proceed anyway.
P.S. We know this message is long but please read the steps above :-) We hope you find them helpful!
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! helloworld@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the helloworld@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/freedoms/.npm/_logs/2019-01-21T16_16_39_054Z-debug.log
A:解決辦法
1、卸載babel-loader
2计螺、指定安裝babel-loader版本8.0.4
3夯尽、重新執(zhí)行npm start
4、坑已填好
五登馒、參考
1匙握、淘寶npm鏡像 http://npm.taobao.org/
2、React 中文文檔 https://react.docschina.org/
3陈轿、React China http://react-china.org/