expo: [expo-branch] null is not an object (evaluating RNBranch.STANDARD_EVENT_ADD_TO_CART

🐛 Bug Report

Environment

This bug happens in Android


  Expo CLI 3.0.9 environment info:
    System:
      OS: macOS 10.14.6
      Shell: 5.3 - /bin/zsh
    Binaries:
      Node: 10.16.0 - ~/.nvm/versions/node/v10.16.0/bin/node
      Yarn: 1.17.3 - /usr/local/bin/yarn
      npm: 6.9.2 - ~/.nvm/versions/node/v10.16.0/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    IDEs:
      Android Studio: 3.4 AI-183.6156.11.34.5522156
      Xcode: 10.3/10G8 - /usr/bin/xcodebuild
    npmPackages:
      expo: ^34.0.1 => 34.0.4 
      react: 16.8.3 => 16.8.3 
      react-native: https://github.com/expo/react-native/archive/sdk-34.0.0.tar.gz => 0.59.8 
    npmGlobalPackages:
      expo-cli: 3.0.9

Steps to Reproduce

import * as whatever from 'expo-branch' OR import whatever from 'expo-branch' OR import { Branch } from 'expo-branch'

  1. npm start
  2. Scan QR code on Expo on Android app
  3. See red screen of death: WhatsApp Image 2019-08-15 at 4 14 01 PM

Expected Behavior

It does not break.

Actual Behavior

It breaks.

Reproducible Demo

https://github.com/AryanJ-NYC/expo-sandbox

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 7
  • Comments: 50 (20 by maintainers)

Most upvoted comments

I’m stuck with the same problem.

I’m trying with SDK 35 and still getting that error on IOS and Android. The error happens importing as

import Branch from 'expo-branch'

and

import Branch from 'react-native-branch'

I read about the Android support here, and followed this instructions.

error: null is not an object (evaluating 'RNBranch.STANDARD_EVENT_ADD_TO_CART')

i’m missing something?

Also, for those interested, I’ve ended up handling it this way:

// /my-branch.js
import Constants from 'expo-constants';

if (Constants.appOwnership !== 'standalone') {
  module.exports = {
    subscribe: console.log,
    createBranchUniversalObject: async () => ({
      showShareSheet: console.log,
    })
  }
}
else{
  module.exports = require('expo-branch')
}

and then import Branch from 'my-branch'. The above code would obviously need to mock all functions called by your code.

Just want some clarification on @sjchmiela reply.

So since there is no support with the Expo client, would that mean in order to test branch functionality (for iOS) we would have to do a standalone build and upload to Testflight?

Is there any other workflow for this at the moment? And is Expo client support for branch something in the works?

Thanks for your help.

Same here. We can´t test in in the iOS Simulator? SDK 35, get the same error in iOS: RNBranch.Standard_Event_Add_to_Card

Branch is only included in standalone apps (expo build:android) and only if at the moment of executing that command you have expo-branch listed among dependencies in package.json. So no support in Expo client. 🙂

Thanks for the reply, @sjchmiela! So is included in the build but can’t be tested first because the app crashes. What about IOS? Is also crashing in Expo client in SDK 35, but works on SDK 34. Seems to me that is the same as before and devs need to eject from expo to use it properly. Or there is a way to integrate it without breaking the app?

Please, correct me if I’m wrong. Thanks

EDIT: Well, @AryanJ-NYC suggestion will work without broking the app. That point is covered.

To import dynamically using await, what worked for us was:

const ExpoBranch = await import('expo-branch')
const Branch = ExpoBranch.default;
Branch.setIdentity('user_id')

Yes, it has, @sjchmiela.

This would mean that, while import { DangerZone } from 'expo' was a safe import, import ... from 'expo-branch' // or react-native-branch is unsafe since RNBranch is null in Expo client. This lead me to use the following pattern:

if (Constants.appOwnership === 'standalone') {
  const { BranchEvent } = await import('expo-branch')
  const branchEvent = new BranchEvent(eventType, null, properties) // assume properties is defined
  branchEvent.logEvent()
}

which now does not return the same error as above. Unfortunately, I’m still unsuccessfully able to run a built app. Will keep updated.

@yodaheis Just be aware that import() returns a Promise. You should probably make it something like…

const someAsyncFunction = async () => {
  if(Constants.appOwnership === 'standalone') {
    const Branch = await import('expo-branch')
    // Your code here ...
  }
}

@davidbiller I don’t know why, but it works now. I import it with regular top import and I left the if (Constants.appOwnership === 'standalone') {.

Thank you! I spent about 5 hours on this.

That’s right, that should do the work! To inspect whether expo-branch has been added to the build, expand the resolve native modules step in expo.io/builds and verify whether Adding expo-branch:1.0.0 is present there. If it is, most probably all the required native modules have been added to the application.

Zrzut ekranu 2019-08-19 o 20 55 09

This hasn’t been communicated clearly anywhere, since we haven’t announced this feature publicly yet, but when trying to use Branch you will need to remember that:

  • nothing has changed when it comes to Expo client — it didn’t have Branch and it won’t. So, trying to import and/or call any Branch methods in Expo client will raise an error, since RNBranch, i. e. native module, is null in Expo client.
  • Branch module is only added to Android standalone apps. expo-branch is a wrapper around react-native-branch which allows us to optionally include it in standalone applications depending on dependencies field in package.json. Branch will be available in your application only in Expo-built apps (expo build:android) and only if you add expo-branch to your NPM dependencies.
  • If you build your app with expo-branch included and importing from expo-branch doesn’t work due to some problem with exports, try importing from react-native-branch, as @cruzach proposed here.
  • As always you can eject to either bare workflow or ExpoKit and add expo-branch/react-native-branch there. 🙂

Has this description helped understand current situation? 😅

This works on iOS but still results in the same screen (shown in OP) on Android.

I’ve updated https://github.com/AryanJ-NYC/expo-sandbox to reflect the changes you suggest.