Stream module in React Native
Asked Answered
R

4

5

I want to use a node library which relies on stream (require('stream')) within a React Native project.

This is failing with error stream could not be found within the project because stream is a nodejs package not available in React Native.

What can I do ?

I read that https://github.com/browserify/stream-browserify allows the use of streams in the browser. But how can I make the require('stream') in the library (that I don't control) require stream-browserify instead.

Roselani answered 1/3, 2021 at 22:4 Comment(0)
P
7

I had the same problem in my ReactNative project and tried many suggestions, but what finally worked for me was to use the resolver: section in metro.config.js.

My issues were with both stream and crypto.

Here is the resolver section that fixed my problems-

resolver: {
  extraNodeModules: {
      stream: require.resolve('readable-stream'),
      crypto: require.resolve('react-native-crypto-js'),
  }
},
Providing answered 14/3, 2021 at 21:48 Comment(2)
where should i add this code?Ideomotor
@ben you should create metro.config.js in root of your project and add it there.Duad
G
3

I got the same error too and ended up installing:

  1. yarn add stream
  2. yarn add events

In my case the issues with dependencies started trying to generate an HMAC with this library: https://github.com/crypto-browserify/createHmac

Gutter answered 25/3, 2022 at 17:0 Comment(0)
G
2

I got the same error and I solved it by just installing the stream:

npm install stream
Guido answered 29/8, 2021 at 13:32 Comment(0)
O
1

Just had a similar issue. You can install the react native-friendly package stream-browserify as stream with npm install stream@npm:stream-browserify

Officialdom answered 4/11 at 5:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.