commerce-sdk-isomorphic icon indicating copy to clipboard operation
commerce-sdk-isomorphic copied to clipboard

Does not run in react-native

Open anutting opened this issue 2 years ago • 8 comments

I installed this module in my react-native project, but receive an error:

error: Error: While trying to resolve module `crypto` from file `my-project/node_modules/commerce-sdk-isomorphic/lib/index.cjs.js`, 

It seems like this module requires node's crypto package that is not available in react-native.

Please provide a workaround, alternative module, or fix for this issue I can use the commerce-sdk in react-native.

Thank you

anutting avatar Jan 12 '23 19:01 anutting

This issue has been linked to a new work item: W-12659143

git2gus[bot] avatar Mar 08 '23 16:03 git2gus[bot]

Hi @anutting, thanks for creating this issue. A work item in our backlog has been created and we'll loop you back in with any updates.

joeluong-sfcc avatar Mar 08 '23 16:03 joeluong-sfcc

Hi, Is there any update on this issue, since I am working on react-native and still facing this issue

asad-pwc avatar Jan 24 '24 18:01 asad-pwc

Hi, Is there any update on this issue, since I am working on react-native and still facing this issue

Hi @asad-pwc, I don't think the team will have time to look into this as we do not normally use react native and would have to ramp up. However it does look like this problem isn't specific to our lib, there is a shim that was created here that you can probably use to resolve your problem. Have you given that a try?

bendvc avatar Feb 16 '24 18:02 bendvc

i already fix this.

The issue its on helpers, because RN did not hava a Crypto lib, insted of expo-crypto.

So we should create an alieas expo-crypto to crypto

To do that install this plugin https://www.npmjs.com/package/babel-plugin-module-resolver and update the configuration on: babel.config.js plugins: [ [ "module-resolver", { alias: { crypto: "expo-crypto", }, }, ], ],

Then on the library we should verify if is RN, Brower or Node.

if ( typeof navigator !== 'undefined' && navigator.product === 'ReactNative' ) { challenge = (await crypto.digestStringAsync?.('SHA-256', codeVerifier, { encoding: 'base64', })) || ''; } else { challenge = urlSafe( crypto.default .createHash('sha256') .update(codeVerifier) .digest('base64') ); }

i will create a PR, but dont know if will get aprroved

FerVillanuevas avatar May 17 '24 19:05 FerVillanuevas

https://github.com/SalesforceCommerceCloud/commerce-sdk-isomorphic/pull/154 PR here

FerVillanuevas avatar May 17 '24 19:05 FerVillanuevas

Hi, was considering this for a React Native app, but upon seeing this am I to assume this library is not fit for use and I should create a from scratch integration with SLAS? Thanks.

samskinner-joann avatar Aug 13 '24 18:08 samskinner-joann

@samskinner-joann That is correct, this package does not support react native out of the box. One potential solution if you don't want to create the SLAS requests from scratch is to fork this repo and make the necessary modifications to get the commerce-sdk-isomorphic working with react native and use the modified library

joeluong-sfcc avatar Aug 14 '24 22:08 joeluong-sfcc