almost/react-native-sqlite

Tried 3 different ways. Can't get this to work

Closed this issue · 15 comments

First, I did:

  1. npm install --save react-native-sqlite while inside my app's directory. It installed inside the node_modules/ inside app directory.
  2. Dragged AIBSQLite.xcodeproj into my project.
  3. Added libAIBSQLite.a into my build phases. But the library was shown in red and xcode build failed without any error messages.

Second,

  1. I went to my home (~) and did sudo npm install react-native-sqlite and it installed in ~/node_modules/.
  2. Added AIBSQLite.xcodeproj to project. libAIBSQLite.a was still red, but it built successfully.
  3. But when I added this line var sqlite = require('./react-native-sqlite') in a file, I got Requiring unknown module error in the simulator.
    I tried restarting the packager. No use. I even tried require('react-native-sqlite'). No use.
    ios simulator screen shot 07-apr-2015 5 14 43 pm

Lastly,

  1. Added react-native-sqlite: '*' to my app's package.json. Did npm install.
  2. Had the same issue from the 1st method. ie, xcode failed without any error messages.

How do I make this work?

I managed to get the error log for when xcode was mysteriously failing build without any error messages. This is the error I got:

AIBSQLite.h:9:9: 'RCTBridgeModule.h' file not found

Trying to get to the bottom of this.

require('react-native-sqlite') is the correct type of require. Make sure you restart the packager (it's running a Terminal window probably) because it's not great at handling changes in node_modules.

You also need to add libsqlite3.dynlib in your build phases.

Do try restarting everything (the old "turn it off and on again" trick). And make sure you just have your project open in xcode (xcode doesn't seem to like a sub project being open in multiple places, but doesn't actually show error messages).

I'm just working on an example project at the moment which I'll publish soon or email me at tom@almostobsolete.net to get the current semi-working version, might help to have that for comparison.

I'll look into the example. Meanwhile I've made a pull request fixing the documentation in the README.md (#10)

Oh, I didn't realise I'd given you the incorrect require in the README example. Sorry about that and thanks for the Pull Request!

@almost The example ran well. But strangely, even if I remove $(SRCROOT)/node_modules/react-native/React from the User Header Paths, it still builds. So where is it getting the headers from?

This works only in your projects. In mine, I've to add $(SRCROOT)/.. with recursive to make it build. Any idea what's going on?

It's the "Header Search Paths" it needs I think, not "User Header Paths". The AIBSQLite xcode project does already have the Header Paths set as well.

@almost Sorry I forgot to have libAIBSQLite.a in my Build Phases. But still, after adding it, I can't get it to work with $(SRCROOT)/node_modules/react-native/React. It only works when I give $(SRCROOT)/../.

Ah, I think your $(SRCROOT ) for your xcode project must be in a subdirectory of your repository.

My directory structure is like so,

MyProject.xcodeproj
package.json
index.ios.js
node_modules/react-native
node_modules/react-native-sqlite/AIBSQLite.xcodeproj

So, $(SRCROOT) for AIBSQLite.xcodeproj is ~/MyProject/node_modules/react-native-sqlite/. Therefore, $(SRCROOT)/node_modules/react-native/React will be ~/MyProject/node_modules/react-native-sqlite/node_modules/react-native/React.

See the problem?

Whereas $(SRCROOT)/../ would mean ~/MyProject/node_modules/react-native-sqlite/../ where react-native is located. And that is why this setting works for me.

Is there anything wrong with my project structure?

When react-native-sqllite is installed by NPM it should get a copy (actually probably just a symlink) of react-native installed at ~/MyProject/node_modules/react-native-sqlite/node_modules/react-native since it has react-native as a dependency. I'm not sure why that isn't happening here...

Probably because react-native is already installed globally (for the CLI). So NPM doesn't feel the need to pull it.

EDIT:
That's not right. I guess react-native isn't installed global. Only the CLI is.

But more importantly, I solved the issue :) I removed a react-native-sqlite which somehow got into /usr/local/lib/node_modules/. Then I removed the package from my project's node_modules and did npm install again. Now there's a react-native in react-native-sqlite's node_modules.

For the import issue, I'm using #import "../react-native/React/Base/RCTBridgeModule.h"

@menway It doesn't work otherwise? I fixed it. Without changing the #import "RCTBridgeModule.h".

I've changed the paths so it now uses the react-native next to it in the node_modules directory. I've changed the dependency to a peer dependency which should make that work.

So give it a try again with the latest version on NPM.