alexindigo / react-native-hypercore

React Native version of the Hypercore module – a secure, distributed append-only log.
MIT License
8 stars 0 forks source link

Unable to resolve module 'fs' #1

Open jvntf opened 5 years ago

jvntf commented 5 years ago

Trying to use react-native-hypercore in a react native app, but when I try to import hypercore from 'hypercore' (have also tried require()), I get the following error:

error: bundling failed: Error: Unable to resolve module `fs` from `/Users/jeevanfarias/Documents/columbia/center_for_spatial_research/distributed_sensing/location_tracker/node_modules/node-gyp-build/index.js`: Module `fs` does not exist in the Haste module map

This might be related to https://github.com/facebook/react-native/issues/4968
To resolve try the following:
  1. Clear watchman watches: `watchman watch-del-all`.
  2. Delete the `node_modules` folder: `rm -rf node_modules && npm install`.
  3. Reset Metro Bundler cache: `rm -rf /tmp/metro-bundler-cache-*` or `npm start -- --reset-cache`.
  4. Remove haste cache: `rm -rf /tmp/haste-map-react-native-packager-*`.

I have installed and linked hypercore, tried installing fs manually, following the above instructions to clear the caches...but it still won't run. Any idea what I am doing wrong? Using versions: npm: 6.4.1 node: 10.15.3 react-native: 0.59.1 (node is outdated because newer versions of node or not compatible with another module I need) Thanks!

alexindigo commented 5 years ago

@jvntf Hey, thanks for you interest. This repo is still work in progress and used as a proof of concept for this issue: https://github.com/mafintosh/hypercore/issues/198

In my test app I have this packages and overrides in the package.json:

  "react-native": {
    "net": "@hawkingnetwork/react-native-tcp",
    "tls": "@hawkingnetwork/react-native-tcp/tls",
    "crypto": "react-native-crypto"
  },
  "dependencies": {
    "@hawkingnetwork/react-native-tcp": "^3.3.2",
    "hypercore": "https://github.com/alexindigo/hypercore.git",
    "hypercore-crypto": "https://github.com/alexindigo/hypercore-crypto.git#master",
    "hypercore-protocol": "https://github.com/alexindigo/hypercore-protocol.git#master",
    "react-native-crypto": "^2.1.2",
    "react-native-gesture-handler": "^1.0.12",
    "react-native-randombytes": "^3.5.1",
    "react-native-sodium": "^0.3.2",

Then I have

hyper-test.js:

import net from 'net';
import hypercore from 'hypercore';

export default function connectHyper(callback) {
  const client = net.createConnection(10000);

  client.on('error', (error) => callback(error));

  const feed = hypercore('./my-test-dataset', '3b72dd08c093544b9763789201802811ea8e944946c9b0af25741dfd050fe5e8', {valueEncoding: 'utf-8'})

  feed.on('error', (err) => callback(err))

  feed.on('ready', function()
  {
    console.log('READY:', feed.key.toString('hex'));

    // console.log('ARCHIVE:', archive.key.toString('hex'));
    // console.log('DISCOVERY:', archive.discoveryKey.toString('hex'));

    feed.get(0, (err, data) => callback(null, data));
    feed.get(1, (err, data) => callback(null, data));

    client.pipe(feed.replicate()).pipe(client)

    // client.on('data', function(data) {
    //   console.log('message was received', data);
    // });
  });
}

And this is piece I have in App.js:

import hyperConnect from './hyper-test';

class App extends Component<Props> {

  constructor(props){
    super(props);
    this.state = { netMessages: [] };
  }

  componentDidMount = () => {
    hyperConnect((err, message) => {
      const netMessages = this.state.netMessages;
      netMessages.push(message);
      this.setState({ netMessages });
    })
  };
}

And it all starts without errors, but I wasn't able to make a successful connection.

And then I got sick and still recovering, so no real progress since then. :)

jvntf commented 5 years ago

thanks @alexindigo! I would guess my problem is based on the overrides. For now I have figured out a work around using nodejs-mobile to handle the hypercore routines

alexindigo commented 5 years ago

@jvntf Interesting, I'd be curious to see what you've got, if there is a chance :)

jvntf commented 5 years ago

Sure, here is the repo which is a bit chaotic and very WIP at the moment but the interesting stuff is here in App.js and in main.js

App.js is the React Native app, it runs main.js as a node-js thread. main.js handles all of the actual hypercore / hyperdiscovery and can pass info back and forth via rn-bridge module (basically a bidirectional pipe. A bit clunky, but seems to get the job done. Haven't stress tested or looked into the security of this yet. Let me know if you what you think!