philikon / ReactNativify

How to use node.js libraries in React Native
244 stars 23 forks source link

Buffer is not defined #5

Closed franleplant closed 7 years ago

franleplant commented 7 years ago

Hi! Thanks a lot for your work.

I've followed the guides but I cannot seem to pass the globals error:

I did add the globals.js and import it from my code

If I change the order of the globals import, I get something like:

In any case, I thought i would be nice to at least contribute in this way to your work. Im actively trying other alternatives to make mqtt work with React Native

wswoodruff commented 7 years ago

@franleplant Yo! I just ran into this issue as well, and I'm trying to bring it in with various babel global plugins with no luck so far. Have you arrived at an answer yet?

wswoodruff commented 7 years ago

Hey @franleplant I got this error to go away just by requiring my globals.js file in the file where I register the app. I do require('../globals.js'); (The main file is in a src/ folder)

globals.js has this in it:

// Inject node globals into React Native global scope.
global.Buffer = require('buffer').Buffer;
global.process = require('process');
global.process.env.NODE_ENV = __DEV__ ? 'development' : 'production';

// Needed so that 'stream-http' chooses the right default protocol.
global.location = {
    protocol: 'file:'
};

You can see a working example here: https://github.com/wswoodruff/strangeluv-native/blob/master/src/index.js#L6

If you have other issues, I ran into problems with crypto.pbkdf2Sync, and made a temporary shim for it until someone comes up with a better solution. You can see #6 and https://github.com/mvayngrib/react-native-crypto/issues/14#issuecomment-306072211 for more info. Please let me know if you get it working!

franleplant commented 7 years ago

Hey @wswoodruff thank you so much for your contribution. Im not actively working on React Native at the moment and I don't have a dev env for it so I'm not going to be testing this soon, but it's great to have this sort of experience documented for future users (even our future selves). So thank you!

flaviouk commented 7 years ago

Was having the same issue, this combined with https://github.com/philikon/ReactNativify/issues/4 was exactly what I needed, thank you so much @wswoodruff @franleplant @philikon @parshap

arshbot commented 6 years ago

Following all the same tools and tactics however I am unable to over this error. It looks like Buffer isn't in the global scope for some reason. Getting Buffer is not defined in a standard library util folder node_modules/core-util-is/lib/util.js on exports.isBuffer = Buffer.isBuffer;

kpanuragh commented 6 years ago

use this package https://www.npmjs.com/package/react-zlib-js and import buffer from react-zlib-js/buffer adn replace "Buffer" with buffer