2

I created a module/library with react-native-create-library for handling specific ble procedures of a custom device and chose react-native-ble-manager for the native ble functionalities. Since I did not write any native code in my library I'm just exporting my module in index.js

import BleHandler from './src/BleHandler.js';

export default BleHandler;

Then I packed my library with npm to locally test it in a test application. Up until here everything is fine and I can import and use my module in the test application. But when I try to initialize the ble module it fails because it appearently is not contained in NativeModules:

[TypeError: null is not an object (evaluating 'bleManager.start')]

I included the ble module in the package.json of my module

"dependencies": {
    "@react-native-community/async-storage": "^1.11.0",
    "react": "16.13.1",
    "react-native": "0.63.3",
    "react-native-ble-manager": "^7.4.1",
    "react-native-fs": "^2.16.6",
    "react-native-windows": "^0.57.2"
  }

But when I checked in:
TestApplication/node_modules/my-module/node_modules
react-native-ble-manager was not there.

My question now is, if it is even possible to use a native module inside a module, and if so, how to do it correctly?

IzayaMe
  • 21
  • 2
  • Are you sure you did the npm install and pod install? – Haseeb A Oct 13 '20 at 11:47
  • Yes I did the npm install. And when I configure my library as an application (adding ui components, and adjusting package.json/index.js), it works just fine. Only when I use it as a library, it wont work. – IzayaMe Oct 14 '20 at 09:27

1 Answers1

1

You should add react-native-ble-manager to peerDependencies in your library instead of in dependencies. Then the user of your library would install the native module in their project under dependencies.

See https://github.com/callstack/react-native-builder-bob#how-do-i-add-a-react-native-library-containing-native-code-as-a-dependency-in-my-library

satya164
  • 9,464
  • 2
  • 31
  • 42