运行React Native项目,终端无法自动打开并报错。报错信息如下:
/Users/cloud/Library/Developer/Xcode/DerivedData/FFProject-epfypvlhidieqsbtkbftzahhoglq/Build/Intermediates/FFProject.build/Debug-iphoneos/FFProject.build/Script-40CA3C721C059E3D0007EDF4.sh: line 2: /Users/cloud/Documents/pingan/PA_shunde: is a directory
The file /Users/cloud/Documents/pingan/PA_shunde 2/App_IOS/FFProject/2/App_IOS/react_native/node_modules/react-native/packager/launchPackager.command does not exist.
Can't start packager automatically
/Users/cloud/Library/Developer/Xcode/DerivedData/FFProject-epfypvlhidieqsbtkbftzahhoglq/Build/Intermediates/FFProject.build/Debug-iphoneos/FFProject.build/Script-40CA3C721C059E3D0007EDF4.sh: line 13: /Users/cloud/Documents/pingan/PA_shunde: is a directory
Command /bin/sh failed with exit code 126
意思是在此目录下没有launchPackager.command这个文件,但是我自己根据目录能够查到这个文件。
后来在工程的Build Phases里面找到了答案。里面有个Run Script选项:
$SRCROOT/../react_native/build.sh $SRCROOT/react-native-build.cfg
if nc -w 5 -z localhost 8081 ; then
if ! curl -s "http://localhost:8081/status" | grep -q "packager-status:running" ; then
echo "Port 8081 already in use, packager is either not running or not running correctly"
exit 2
fi
else
open $SRCROOT/../react_native/node_modules/react-native/packager/launchPackager.command || echo "Can't start packager automatically"
fi
$SRCROOT/../react_native/react-native-xcode.sh $SRCROOT/../react_native/
由此可以看出是这个脚本没有运行从而报错“Can't start packager automatically”
只需将下面Run script only when installing打钩即可。
原本以为这样修改之后就没有问题了,但是发现这样修改虽然build成功了,但是运行起来会报红屏。“未能完成操作,loadJSBundle is nil”;
所以上面打钩只能build成功,并不能解决根本问题,后来google之后在Facebook官网上找到了答案。是我的目录名称里面含有空格导致的。我的工程命名为PA_shunde 2文件夹。
I discovered the problem. One of the directories that the project folder was in had a space in its name. As soon as I removed the space, hitting command + R automatically ran the npm server!