React Native安装步骤
前期准备
- 安装Homebrew
mac OS X 软件安装包管理工具
$ curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install - 安装node.js 4.0或更高版本
- 安装nvm
- $ nvm install node && nvm alias default node
- 安装watchman
facebook的开源项目,用来监视文件并记录文件的改动情况
$ brew install watchman - 安装flow
$ brew install flow
安装react native
- 敲入命令行
$ npm install -g react-native-cli - 选择路径创建工程:
$ react-native init AwesomeProject
这段时间会比较长,需要下载一些初始化文件 - 运行:
Android:$ react-native run-android
Q&A
1. 启动node server报错
Failed to build DependencyGraph: Cannot read property 'root' of nullTypeError: Cannot read property 'root' of null at index.js:16:60
at tryCallOne (/Users/liying/react_test/node_modules/react-native/node_modules/promise/lib/core.js:37:12)
at /Users/liying/react_test/node_modules/react-native/node_modules/promise/lib/core.js:123:15
at flush (/Users/liying/react_test/node_modules/react-native/node_modules/promise/node_modules/asap/raw.js:50:29)
at doNTCallback0 (node.js:430:9)
at process._tickCallback (node.js:359:13)~
Process terminated. Press <enter> to close the window
解决方案:
- $ brew update
-
$ brew upgrade watchman
如果在此过程中遇到报错:
Error: The /usr/local directory is not writable.
Even if this directory was writable when you installed Homebrew, other
software may change permissions on this directory. Some versions of the
"InstantOn" component of Airfoil are known to do this.
You should probably change the ownership and permissions of /usr/local
back to your user account.
sudo chown -R $(whoami):admin /usr/local
则输入:*$ sudo chown -R liying:admin /usr/local *
2. 模拟器运行报错
ReferenceError-Can-t-find-variable-fbBatchedBridge
可能原因:模拟器与js服务器不在同一网段,无法访问服务器。
解决办法:打开模拟器中的settings,wifi设置中选择WiredSSID链接。
如果在真机上,摇一摇调出设置menu,选择Dev Setting,选择Debug server host & port for device,确保真机与电脑在同一网段,输入电脑IP+服务器默认端口号(8081) eg. 10.180.141.23:8081