Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Native "Unable to resolve module http" (Advise needed) #362

Closed
RNEvok opened this issue Mar 9, 2023 · 4 comments
Closed

React Native "Unable to resolve module http" (Advise needed) #362

RNEvok opened this issue Mar 9, 2023 · 4 comments

Comments

@RNEvok
Copy link

RNEvok commented Mar 9, 2023

Hello guys. Thank you for awesome lib! It works fine in my Node.js project. But I wonder, is there any instruction how to use it with RN? Currently, I am getting error: "Unable to resolve module http" when trying to use ClientRequestInterceptor.

Reading other issues, I understood that RN support is offered. Maybe there is special branch for RN or something?

p.s. Only FetchInterceptor works fine but I need Interceptor that sees http.request(s)

RN version: 0.66.4
Снимок экрана 2023-03-10 в 02 46 13

@kettanaito
Copy link
Member

Hey, @RNEvok. Thanks for raising this.

I believe that React Native doesn't have the standard http module. What you may be using only has a compatible signature but is implemented by other means.

MSW ships with only XMLHttpRequestInterceptor for React Native and it seems to be covering the use cases people want. Try that interceptor instead, I suspect http may be implemented by XHR in React Native.

Please let me know if that helps.

@RNEvok
Copy link
Author

RNEvok commented Mar 10, 2023

Just tried XMLHttpRequestInterceptor. I am getting this error: "TypeError: Cannot read property 'prototype' of undefined, js engine: hermes". Currently I am trying to get more information of this error. Starting project with xCode did not gave me any additional information.

The code i am trying to run:
`import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest';

const interceptor = new XMLHttpRequestInterceptor();

interceptor.apply()

interceptor.on('request', (request, requestId) => {
console.log(request.method, request.url)
})

interceptor.on('response', (response, request) => {
console.log('response to %s %s was:', request.method, request.url, response)
})`
2023-03-10 19 49 20

I'll try to get more information of this error and I'll edit comment if I know anything.

Edit: I've just managed to get this one:
2023-03-10 19 56 14

Looks like this line in bufferUtils is giving the error const encoder = new TextEncoder(). I'll try to install RN polyfill for "util".

Edit: I've managed to start a project with "react-native-polyfill-globals". But if I apply() XMLHttpRequestInterceptor, any requests stops working. I keep figuring out...

@kettanaito
Copy link
Member

Sorry, I don't have enough insight into React Native to advise here.

Perhaps take a look at the RN usage example one of our contributors prepared mswjs/examples#60 maybe you will find a missing piece there.

@RNEvok
Copy link
Author

RNEvok commented Mar 15, 2023

Thank you for answers.
For now, I decided to defer using this package for ReactNative projects.
Currently I am using it with React and node.js projects.

There is some notes that might be helpful for future readers:
ClientRequestInterceptor works fine with node.js
and
XMLHttpRequestInterceptor works fine with React
For RN I've decided to use react-native/Libraries/Network/XHRInterceptor

@RNEvok RNEvok closed this as completed Mar 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants