React Native support for ofetch
homersimpsons opened this issue · comments
Environment
- react-native: 0.72.4
- ofetch: 1.3.3
- node-fetch-native: main branch (97bcf42aae94794d5cae6ef63c17bfa6dfb3e128)
Reproduction
Checkout following repository: https://github.com/homersimpsons/react-native-ofetch-demo
- initiate a new react-native-cli repository
yarn add ofetch
- Use
ofetch
in your code
Describe the bug
Using published version of node-fetch-native (1.4.0):
Reproduction commit: homersimpsons/react-native-ofetch-demo@d62a7b7
The bundle fail to compile with the following error:
error: Error: Unable to resolve module node:http from /home/user/project/node_modules/ofetch/dist/node.cjs: node:http could not be found within the project or in these directories:
node_modules
../../../node_modules
1 | 'use strict';
2 |
> 3 | const http = require('node:http');
| ^
4 | const https = require('node:https');
5 | const nodeFetch = require('node-fetch-native');
6 | const fetch$1 = require('./shared/ofetch.92c14be7.cjs');
at ModuleResolver.resolveDependency (/home/user/project/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:139:15)
at DependencyGraph.resolveDependency (/home/user/project/node_modules/metro/src/node-haste/DependencyGraph.js:277:43)
at Object.resolve (/home/user/project/node_modules/metro/src/lib/transformHelpers.js:169:21)
at Graph._resolveDependencies (/home/user/project/node_modules/metro/src/DeltaBundler/Graph.js:473:35)
at Graph._processModule (/home/user/project/node_modules/metro/src/DeltaBundler/Graph.js:261:38)
at async Graph._addDependency (/home/user/project/node_modules/metro/src/DeltaBundler/Graph.js:372:20)
at async Promise.all (index 6)
at async Graph._processModule (/home/user/project/node_modules/metro/src/DeltaBundler/Graph.js:322:5)
at async Graph._addDependency (/home/user/project/node_modules/metro/src/DeltaBundler/Graph.js:372:20)
at async Promise.all (index 2)
Using main branch of node-fetch-native (97bcf42aae94794d5cae6ef63c17bfa6dfb3e128):
Reproduction commit: homersimpsons/react-native-ofetch-demo@2168936
The bundle fail to compile with the following error:
error: Error: Unable to resolve module node:http from /home/user/project/node_modules/ofetch/dist/node.cjs: node:http could not be found within the project or in these directories:
node_modules/ofetch/node_modules
node_modules
../../../node_modules
1 | 'use strict';
2 |
> 3 | const http = require('node:http');
| ^
4 | const https = require('node:https');
5 | const nodeFetch = require('node-fetch-native');
6 | const fetch$1 = require('./shared/ofetch.92c14be7.cjs');
at ModuleResolver.resolveDependency (/home/user/project/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:139:15)
at DependencyGraph.resolveDependency (/home/user/project/node_modules/metro/src/node-haste/DependencyGraph.js:277:43)
at Object.resolve (/home/user/project/node_modules/metro/src/lib/transformHelpers.js:169:21)
at Graph._resolveDependencies (/home/user/project/node_modules/metro/src/DeltaBundler/Graph.js:473:35)
at Graph._processModule (/home/user/project/node_modules/metro/src/DeltaBundler/Graph.js:261:38)
at async Graph._addDependency (/home/user/project/node_modules/metro/src/DeltaBundler/Graph.js:372:20)
at async Promise.all (index 0)
at async Graph._processModule (/home/user/project/node_modules/metro/src/DeltaBundler/Graph.js:322:5)
at async Graph._traverseDependenciesForSingleFile (/home/user/project/node_modules/metro/src/DeltaBundler/Graph.js:249:5)
at async Graph.traverseDependencies (/home/user/project/node_modules/metro/src/DeltaBundler/Graph.js:157:9)
Additional context
This is a follow-up of the following issue unjs/node-fetch-native#91
@pi0 You may want to follow-up here
Logs
No response
I noticed in your error log that the error is occurring in node_modules/ofetch/dist/node.cjs
, which isn't the intended file. I checked your metro.config.js and it doesn't look like you've enabled the beta package exports feature. The docs say that it will be enabled by default from v0.73.
I also need this in my React Native app and can check myself in the next couple of days :)
After enabling unstable_enablePackageExports
in metro.config.js
, this package installs without a problem on 0.72. However, all responses are returning undefined
. I attached an onResponse
interceptor to see what was going on, and the status code is returning 200 as expected, the body is just not being returned.
const data = await ofetch('https://reqres.in/api/users?page=2', {
async onResponse({request, response, options}) {
// Log response
console.log(
'[fetch response]',
request,
response,
response.status,
response.body,
);
},
});
console.log(data);
Thanks for investigating dear @gtjamesa. Do you think you can assemble a minimal repository? I had been planning to investigate on this but always short on time to make an initial setup.