转载请注明出处:http://www.wangxinarhat.com/2016/05/21/2016-05-21-react-native1/
摸索React Native有一段时间了,尝试在Windows下和linux下搭建开发环境,踩过的坑仍是会忘,在此记录html
nodejsnode
sudo apt-get install -y build-essential curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - sudo apt-get install -y nodejs sudo ln -s /usr/bin/nodejs /usr/bin/node
React Native命令工具react
npm install -g react-native-cli
Watchman
Watchman是由Facebook提供的监视文件系统变动的工具。安装此工具能够提升开发时的性能(packager能够快速捕捉文件的变化从而实现实时刷新)linux
git clone https://github.com/facebook/watchman.git cd watchman git checkout v4.5.0 ./autogen.sh ./configure make sudo make install
Flow
Flow是一个静态的JS类型检查工具,这一语法并不属于ES标准,只是Facebook自家的代码规范。android
npm install -g flow-bin
将ANDROID_HOME环境变量指向Android SDK的路径,ANDROID_NDK指向NDK目录git
具体的作法是把下面的命令加入到~/.bashrc、~/.bash_profile文件中github
export ANDROID_HOME=~/Android/Sdk
export ANDROID_NDK=~/Android/android-ndk-r10e
shell
使用下列命令使其当即生效(不然重启后才生效):source ./bash_profile
npm
初始化一个React Native的Hello World项目并运行react-native
react-native init HelloReact cd HelloReact react-native run-android
正常的话这是该有一个这样的界面
修改下index.android.js文件,手机端reload js,体验React Native
运行react-native run-android
后手机出现Could not connect to development server
以下图:
查看github找到一个issue,解决方法是:
A common issue is that the packager is not started automatically when you run react-native run-android. You can start it manually using:
就是说运行react-native run-android
,Packger可能不会自动运行,需手动启动它react-native start
watchman issue : Poison: inotify_add_watch
出现如下错误
ERROR A non-recoverable condition has triggered. Watchman needs your help! The triggering condition was at timestamp=1466189741: inotify-add-watch(/home/wangxinarhat/ReactProjects/HelloReact/node_modules/react-native/node_modules/fbjs-scripts/node_modules/babel/node_modules/chokidar/node_modules/anymatch/node_modules/micromatch/node_modules/braces/node_modules) -> The user limit on the total number of inotify watches was reached; increase the fs.inotify.max_user_watches sysctl All requests will continue to fail with this message until you resolve the underlying problem. You will find more information on fixing this at https://facebook.github.io/watchman/docs/troubleshooting.html#poison-inotify-add-watch {"watchmanResponse":{"version":"4.1.0","error":"A non-recoverable condition has triggered. Watchman needs your help!\nThe triggering condition was at timestamp=1466189741: inotify-add-watch(/home/wangxinarhat/ReactProjects/HelloReact/node_modules/react-native/node_modules/fbjs-scripts/node_modules/babel/node_modules/chokidar/node_modules/anymatch/node_modules/micromatch/node_modules/braces/node_modules) -> The user limit on the total number of inotify watches was reached; increase the fs.inotify.max_user_watches sysctl\nAll requests will continue to fail with this message until you resolve\nthe underlying problem. You will find more information on fixing this at\nhttps://facebook.github.io/watchman/docs/troubleshooting.html#poison-inotify-add-watch\n"}} Error: A non-recoverable condition has triggered. Watchman needs your help! The triggering condition was at timestamp=1466189741: inotify-add-watch(/home/wangxinarhat/ReactProjects/HelloReact/node_modules/react-native/node_modules/fbjs-scripts/node_modules/babel/node_modules/chokidar/node_modules/anymatch/node_modules/micromatch/node_modules/braces/node_modules) -> The user limit on the total number of inotify watches was reached; increase the fs.inotify.max_user_watches sysctl All requests will continue to fail with this message until you resolve the underlying problem. You will find more information on fixing this at https://facebook.github.io/watchman/docs/troubleshooting.html#poison-inotify-add-watch at ChildProcess.<anonymous> (/home/wangxinarhat/ReactProjects/HelloReact/node_modules/react-native/node_modules/sane/node_modules/fb-watchman/index.js:206:21) at emitTwo (events.js:87:13) at ChildProcess.emit (events.js:172:7) at maybeClose (internal/child_process.js:827:16) at Socket.<anonymous> (internal/child_process.js:319:11) at emitOne (events.js:77:13) at Socket.emit (events.js:169:7) at Pipe._onclose (net.js:477:12)
由于inotify默认内核参数值过小,修改之:
echo 256 | sudo tee -a /proc/sys/fs/inotify/max_user_instances echo 32768 | sudo tee -a /proc/sys/fs/inotify/max_queued_events echo 65536 | sudo tee -a /proc/sys/fs/inotify/max_user_watches watchman shutdown-server