Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
fix(bridge): improve missing native module error (segmentio#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
fathyb authored Oct 19, 2018
1 parent 5971ff6 commit 0a03617
Show file tree
Hide file tree
Showing 7 changed files with 2,593 additions and 300 deletions.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,62 @@ Please note, if you are choosing to not use a dependency manager, you must keep
```

6. `pod install`

### "Failed to load [...] native module"

If you're getting a `Failed to load [...] native module` error, it means that some native code hasn't been injected to your native project.

#### iOS

If you're using Cocoapods, check that your `ios/Podfile` file contains the right pods :
- `Failed to load Analytics native module`, look for the core native module:
```ruby
pod 'RNAnalytics', :path => '../node_modules/@segment/analytics-react-native'
```
- `Failed to load [...] integration native module`, look for the integration native module, example with Google Analytics:
```ruby
pod 'RNAnalyticsIntegration-Google-Analytics', :path => '../node_modules/@segment/analytics-react-native-google-analytics'
```
Also check that your `Podfile` is synchronized with your workspace, run `pod install` in your `ios` folder.
If you're not using Cocoapods please check that you followed the [iOS support without CocoaPods](#ios-support-without-cocoapods) instructions carefully.

#### Android

Check that `android/app/src/main/.../MainApplication.java` contains a reference to the native module:

- `Failed to load Analytics native module`, look for the core native module:

```java
import com.segment.analytics.reactnative.core.RNAnalyticsPackage;
// ...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
// ...
new RNAnalyticsPackage()
);
}
```

- `Failed to load [...] integration native module`, look for the integration native module, example with Google Analytics:

```java
import com.segment.analytics.reactnative.integration.google.analytics.RNAnalyticsIntegration_Google_AnalyticsPackage;
// ...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
// ...
new RNAnalyticsIntegration_Google_AnalyticsPackage()
);
}
```
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
"lint-staged": "^7.2.0",
"npm-run-all": "^4.1.3",
"prettier": "^1.14.2",
"react": "16.6.0-alpha.8af6728",
"react-native": "^0.57.3",
"rimraf": "^2.6.2"
}
}
23 changes: 23 additions & 0 deletions packages/core/src/__tests__/bridge.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const forceRequire = (): typeof import('../bridge') => {
jest.resetModules()

return require.requireActual('../bridge')
}

it('should throw an error if the core native module is not linked', () => {
jest.setMock('react-native', {
NativeModules: {}
})

expect(forceRequire).toThrow(/Failed to load Analytics native module./)
})

it('should export the core native module', () => {
const RNAnalytics = {}

jest.setMock('react-native', {
NativeModules: { RNAnalytics }
})

expect(forceRequire().default).toBe(RNAnalytics)
})
4 changes: 4 additions & 0 deletions packages/core/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { NativeModules } from 'react-native'

import bridge = NativeModules.RNAnalytics

if (!bridge) {
throw new Error('Failed to load Analytics native module.')
}

export default bridge
export type JsonMap = NativeModules.RNAnalytics.JsonMap
export type Bridge = typeof bridge
1 change: 1 addition & 0 deletions packages/integrations/src/gen-integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ async function prepareJs({
),
template('index.js', {
nativeModule,
name,
disable_ios: String(ios.disabled || false),
disable_android: String(android.disabled || false)
}),
Expand Down
14 changes: 11 additions & 3 deletions packages/integrations/template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ var disabled =
? '{{disable_android}}' === 'true'
: true

module.exports = disabled
? { disabled: true }
: ReactNative.NativeModules['{{{nativeModule}}}'].setup
if (disabled) {
module.exports = { disabled: true }
} else {
var bridge = ReactNative.NativeModules['{{{nativeModule}}}']

if (!bridge) {
throw new Error('Failed to load {{{name}}} integration native module')
}

module.exports = bridge.setup
}
Loading

0 comments on commit 0a03617

Please sign in to comment.