solana-labs / solana-web3.js

Solana JavaScript SDK
https://solana-labs.github.io/solana-web3.js
MIT License
2.13k stars 853 forks source link

superstruct dependency and react native #1002

Closed roccomuso closed 1 year ago

roccomuso commented 3 years ago

solana-web3.js right now doesn't work in react-native environment due to https://github.com/ianstormtaylor/superstruct/issues/86

A different superstruct bundle without the .cjs extension should be used. Or it could be replaced entirely with a different dependency.

jstarry commented 3 years ago

Hey @roccomuso, could you please provide some more info on the issue that you're hitting for react native? The superstruct issue you linked doesn't seem to have anything to do with react native so I'm not sure what exactly the problem is, thanks!

roccomuso commented 3 years ago

Build fails when included in create-react-app.

That issue regarding superstruct could be find in many other repos

roccomuso commented 3 years ago

error: bundling failed: Error: While trying to resolve module superstruct from file node_modules/solana-web3.js/lib/validator-info.js, the package node_modules/superstruct/package.json was successfully found. However, this package itself specifies a main module field that could not be resolved (node_modules/superstruct/lib/index.cjs. Indeed, none of these files exist:

jstarry commented 3 years ago

I see, looks like this issue is relevant and has been fixed: https://github.com/ianstormtaylor/superstruct/issues/404 just need to update superstruct in web3 now

leolangen commented 3 years ago

Based on this comment it looks like the change will not work for RN: https://github.com/ianstormtaylor/superstruct/issues/404#issuecomment-753378281

I managed to get it to work on our app using a similar workaround to this: https://github.com/ianstormtaylor/superstruct/issues/404#issuecomment-753379956

We have a different version of metro-config in our project, using RN version 0.61.2

in the file metro.config.js in the root of the project, I added the cjs extension as follows:

...
const defaultSourceExts = require('metro-config/src/defaults/defaults').sourceExts 

module.exports = {
  ...
  resolver: {
    sourceExts: [...defaultSourceExts, 'cjs'],
    ...
  }
}
steveluscher commented 1 year ago

This is resolved!

github-actions[bot] commented 1 year ago

Because there has been no activity on this issue for 7 days since it was closed, it has been automatically locked. Please open a new issue if it requires a follow up.