superstruct icon indicating copy to clipboard operation
superstruct copied to clipboard

Rename files to `cjs.js` for compatibility with React Native

Open steveluscher opened this issue 1 year ago • 8 comments

Importing @solana/web3.js in React Native throws the following error.

error: Error: While trying to resolve module `superstruct` from file `/Users/sluscher/mobile-wallet-adapter/js/node_modules/@solana/web3.js/lib/index.browser.cjs.js`, the package `/Users/sluscher/mobile-wallet-adapter/js/node_modules/superstruct/package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`/Users/sluscher/mobile-wallet-adapter/js/node_modules/superstruct/lib/index.cjs`. Indeed, none of these files exist:

  * /Users/sluscher/mobile-wallet-adapter/js/node_modules/superstruct/lib/index.cjs(.native|.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)

You can see that the React Native packager expects a .js extension in addition to .cjs

If superstruct could rename the CommonJS bundles from .cjs to .cjs.js that would make it Just Work™ for React Native folks!

More details on the problem here, if you're interested: https://github.com/facebook/metro/issues/535

steveluscher avatar Jul 07 '22 17:07 steveluscher

@steveluscher interesting, I'd be down for a PR if you want to make this change!

ianstormtaylor avatar Jul 07 '22 21:07 ianstormtaylor

For sure. I would have done that in the moment, but I had too much mental stack to switch. If I get to it first, cool; if someone reading this wants to take a crack at it, cooler!

steveluscher avatar Jul 08 '22 04:07 steveluscher

PR'd.

steveluscher avatar Jul 08 '22 23:07 steveluscher

Hi everyone, thanks very much for addressing this! I was having this issue using solana/web3.js and stumbled upon this. I downloaded @steveluscher's branch and tested it out locally and it was able to build.

momolarson avatar Jul 10 '22 02:07 momolarson

Awesome! Let's shipit!

steveluscher avatar Jul 10 '22 04:07 steveluscher

Hi @steveluscher I'm experiencing the exact same problem: iOS Bundling failed 409ms While trying to resolve module superstruct from file... I have installed your branch yarn add git+https://github.com/steveluscher/superstruct.git#react-native-compatibility but still get the same error. Do you by any chance know if there's some additional configuration to do/link me to your "Getting Started" README ?

Thanks a lot in advance!

valentinmadrid avatar Aug 03 '22 12:08 valentinmadrid

@valentinmadrid the package needs to be built, it can't be cloned directly from git. not sure if this is the right way, but i used the same yarn add ... as you, but then also cloned steve's branch to a separate location. in that directory, i ran yarn install && yarn build. this produced a directory named lib. i copied that lib directory to my project's node_modules/superstruct/ directory.

this seems to have worked but i immediately ran into other issues. i don't think they're related though.

inhumantsar avatar Aug 04 '22 22:08 inhumantsar

We'll just have to be patient! Once @ianstormtaylor mashes the merge button on #1086 and does a release, we should all be able to update to that new release from npm.

steveluscher avatar Aug 04 '22 22:08 steveluscher

Hi @ianstormtaylor, we have run into the same issue and really expect a new release of superstruct to address this. Thanks heaps!

charIeszhao avatar Aug 30 '22 03:08 charIeszhao

Guys any resolution, i'm facing exactly same error:

"react-native": "0.69.6",
"@solana/web3.js": "1.66.1"

AndonMitev avatar Oct 25 '22 10:10 AndonMitev

@AndonMitev: https://twitter.com/steveluscher/status/1565232097044099073

steveluscher avatar Oct 25 '22 15:10 steveluscher

Thanks for this but i'm on "@solana/web3.js": "1.66.1" with react-native: "0.69.6"

AndonMitev avatar Oct 26 '22 11:10 AndonMitev

I know! You’ll have to upgrade to 0.70+ for support going forward. We’ve abandoned the 0.69- line.

steveluscher avatar Oct 26 '22 18:10 steveluscher