初试React(基于homebrew1.8.6、node11.6.0、npm6.5.0、cnpm6.0.0)

一、开发环境搭建

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[全局安装参数] 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 [email protected]
    解释:
    cnpm install[安装命令] [email protected] [软件包名@版本号](版本号为可选参数,不添加则自动下载最新版本)

7、安装react

    命令:
    cnpm install react
    解释:
    cnpm install [安装命令] react[软件包名]

8、安装react-dom

    命令:
    cnpm install react-dom
    解释:
    cnpm install[安装命令] react-dom[软件包名]

二、创建第一个react工程

1、创建基础工程

    命令:
    create-react-app helloworld
    解释:
    使用create-react-app创建一个名为helloworld的工程目录

2、拿出一包咖啡泡上

    命令:
    别放糖,少放奶
    解释:
    解释?解释什么?会下载很多东西,喝杯咖啡等着就行了

3、没了

三、运行helloworld

1、进入helloworld工程目录,启动工程

    命令:
    cd helloworld
    npm start
    解释:
    进入helloworld工程目录
    启动工程

2、查看运行效果

    浏览器输入http://localhost:3000
    若正常启动浏览器将显示一个react页面

四、坑

1、Q:npm start执行失败,日志如下

  [@Freedoms:helloworld (master)]$ npm start
  > [email protected] 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! [email protected] start: `react-scripts start`
  npm ERR! Exit status 1
  npm ERR!
  npm ERR! Failed at the [email protected] 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、重新执行npm start
  4、坑已填好

五、参考

1、淘宝npm镜像 http://npm.taobao.org/
2、React 中文文档 https://react.docschina.org/
3、React China http://react-china.org/

你可能感兴趣的:(初试React(基于homebrew1.8.6、node11.6.0、npm6.5.0、cnpm6.0.0))