ReactNative 初始化一个新项目,遇到的问题

错误一:

SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable

解决报错的方法:
从已有的androidStudio根目录中,复制一份local.properties到react-native项目android目录中

错误二:

unable to load script from assets ‘index.android bundle’ ,make sure your bundle is packaged correctly or youu’re runing a packager server

解决报错的方法:

第一步:在Android/app/src/main目录下创建一个空的assets文件夹,

第二步:进入项目根目录执行下面代码:

react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/ 

第三步:重新运行程序完美解决。

总结:
index.android.bundle是用来调用原生控件的js脚本,每次当改变了 index.android.js,都需要使用上面的代码片段,来及时的更新index.android.bundle,然后打包才可以把新的index.android.js应用上,所以当没有index.android.bundle文件时,React-Native 项目是无法运行的。

错误三:

Cannot find module node_modules\react-native\local-cli\cli.js'

解决报错的方法:
npm install ,然后:npm install重新安装以后,配置

npm config set registry https://registry.npm.taobao.org --global  
npm config set disturl https://npm.taobao.org/dist --global 

错误四:

React Native:真机调试+跨域资源加载出错问题解决

解决报错的方法 一:
将localhost替换成本机IP,如IP:8081,也就是说,我们通过http://192.168.3.126:8081/debugger-ui/ 来访问调试界面。或者都替换成localhost访问。

解决方法二:CORS
找到node_modules/metro模块,修改Server/index.js、index.js.flow文件,在_processDeltaRequest方法里加上下面代码。
mres.setHeader(“Access-Control-Allow-Origin”, “*”);
这个方法不推荐,不过如果急着调试的话也不妨试下

错误五:
npm install 报错:NPM Unexpected end of JSON input while parsing near
解决方法:
npm install –registry=https://registry.npm.taobao.org –loglevel=silly
npm cache clean –force

npm ERR! code EINTEGRITY 解决方案
挨个试一下:
npm cache verify
npm cache clean
npm cache clean –force
npm i -g npm
grep -ir “sha1-xxxxxxxxxxxxxxxx” ~/.npm

你可能感兴趣的:(reactnative)