stripe-react-native: Expo managed workflow - crash on web

Describe the bug Importing dependencies from @stripe/stripe-react-native causes app to crash on Expo managed workflow - web.

To Reproduce Steps to reproduce the behavior:

  1. Create new expo managed workflow project
  2. expo install @stripe/stripe-react-native
  3. import stripe dependencies into component
  4. expo start --web
  5. See errors

Expected behavior No errors unless stripe components are actually used at runtime.

Additional context I’m aware that web isn’t supported, but it would be nice if it would at least build so I could run my app. I don’t plan to render any stripe components in the browser at runtime. Currently I can’t figure out a way to make Webpack exclude this dependency. Any help would be greatly appreciated.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 3
  • Comments: 44

Most upvoted comments

Let me make the steps explicit for the lazy folks like future me. (All credits to @robert-nash , in his previous answer.)

  1. Add null-loader

    yarn add -D null-loader
    
  2. Generate the webpack.config.js file with expo’s template:

    npx expo customize webpack.config.js
    
  3. Modify webpack.config.js, add the line marked with the comment ADD THIS LINE. Final version of the file should look like:

    // webpack.config.js
    const createExpoWebpackConfigAsync = require('@expo/webpack-config');
    
    module.exports = async function (env, argv) {
    	const config = await createExpoWebpackConfigAsync(env, argv);
    	// Customize the config before returning it.
    	config.resolve.alias["@stripe/stripe-react-native"] = "null-loader"; // ADD THIS LINE
    	return config;
    };
    

I am still working through the first web build of my app but I seem to have got round this particular error for the meantime by adding null-loader as an alias for @stripe/stripe-react-native in my webpack config.

I added a webpack.config.js as described here and then modified it so it looks like this excerpt. I also installed the null-loader package.

webpack.config.js

const createExpoWebpackConfigAsync = require('@expo/webpack-config');

module.exports = async function (env, argv) {
  const config = await createExpoWebpackConfigAsync(env, argv);
  config.resolve.alias["@stripe/stripe-react-native"] = "null-loader";
  return config;
};

No. All the errors were introduced when I added the Stripe library.

@sfriedman-stripe No one here is asking you to support the web with your React Native SDK. It would be nice however if you made it so that having the SDK listed as a dependency in the package.json file doesn’t make the application crash when you run it in web mode. Until that has been fixed, I think you should leave this open.

@paulmbw @heinrichcoetzee

Sorry for the late response. When you make stripe.native.js and stripe.js, you just import /stripe and it will import the correct module depending on the environment where the code is run (web or mobile)

For clarity, here are my files:

stripe.tsx

// DO NOT IMPORT THIS FILE, import the index.ts instead

// Since stripe-react-native is not available on web, this just has non-functioning
// placeholders

import { PropsWithChildren } from 'react'

type StripeProviderProps = {
    publishableKey: string
}

export const StripeProvider = ({ publishableKey, children }: PropsWithChildren<StripeProviderProps>) => {
    return (
        <>
         {children}
        </>
    )
}

type StripeHook = (() => null) |
(() => {
    initPaymentSheet: (context: any) => { error: any }
    presentPaymentSheet: () => { error: any }
})

export const useStripe: StripeHook = () => null;

stripe.native.tsx

// DO NOT IMPORT THIS FILE, import the index.ts instead

// Core
import { StripeProvider, useStripe } from '@stripe/stripe-react-native';

export { StripeProvider, useStripe }

index.ts

import { StripeProvider, useStripe } from './stripe';

export { StripeProvider, useStripe }

Then you just import the index.ts file and the appropriate stripe file will be loaded depending on your environment. I think the reason I had an index.ts file was just so I could import from the folder instead of file, like /stripe instead of /stripe/stripe, but if you don’t have a separate stripe folder then I think you can skip the index.ts file and just import /stripe.

Sorry if this is confusing, if so let me know and I’ll try to clarify.

Hi all, we don’t plan on supporting the web with our React Native SDK. Please use Stripe.js if you are building a website. I will be closing this down. Please feel free to open a new issue if you have any further questions

I’ve asked the devs who maintain expo as well for guidance on this as I’m also running into this issue https://x.com/paulwaweruco/status/1797291557617340591

I managed to solve this by using Platform extensions. I created a file stripe.ts for web which exports placeholder components/functions, and created a file stripe.native.ts for mobile which exports the actual stripe components/functions. Now I can import by ./stripe and it will automatically import the correct module based on which platform I’m using.

@JorensM I’ve tried your solution. I’ve got a stripe.native.tsx file which exports the actual stripe components/functions, and I’ve got a stripe.web.tsx with placeholders.

I then import both files in a StripeProvider and decide which file to render based on the Platform:

import { Platform } from "react-native";
import { StripeProvider as WebStripeProvider } from "./stripe.web";
import { StripeProvider as NativeStripeProvider } from "./stripe.native";

type StripeProviderProps = {
  children: JSX.Element | JSX.Element[];
};

const StripeProvider: React.FC<StripeProviderProps> = ({
  children,
}: StripeProviderProps): JSX.Element => {
  const publishableKey = process.env.EXPO_PUBLIC_STRIPE_PUBLISHABLE_KEY;

  if (!publishableKey) {
    throw new Error(
      "publishableKey is not set. Ensure that EXPO_PUBLIC_STRIPE_PUBLISHABLE_KEY is set in your environment variables."
    );
  }

  if (Platform.OS === "web") {
    return (
      <WebStripeProvider publishableKey={publishableKey}>
        {children}
      </WebStripeProvider>
    );
  }

  return (
    <NativeStripeProvider publishableKey={publishableKey}>
      {children}
    </NativeStripeProvider>
  );
};

export default StripeProvider;

I’m still running into the same error:

Unable to resolve module ../../Utilities/Platform from /Users/paulwaweru/Projects/launchtodayhq/launchtoday/node_modules/react-native/Libraries/Components/TextInput/TextInputState.js

Is there something we might be missing?

This is seriously still an issue…

@olaurendeau, hello thank you for the suggestion! If you have time, can you explain more about the dependency injection container to abstract away the platform dependent components ?

New solution works, but lazy TL;DR for anyone facing the problem on Expo Web (since webpack doesn’t seem to be supported anymore):

  1. Find your .js or .tsx files with Stripe
  2. Make a .web.js or .web.tsx copy of those files
  3. Remove Stripe functions, then add in dummied out functions in their place

Preferably for a larger project, you might want to abstract out your Stripe functions instead, then make components that return the exact functionality (on mobile) or a dummied out function( on web), instead of maintaining two identical versions of your pages.

Relevant documentation on platform specific extensions here: https://docs.expo.dev/router/advanced/platform-specific-modules/#platform-specific-extensions

Awesome, thank you @JorensM !

I’ve written up a blog post to expand on this further, let me know if you have any questions

https://blog.launchtoday.dev/article/integrate-stripe-expo

I managed to solve this by using Platform extensions. I created a file stripe.ts for web which exports placeholder components/functions, and created a file stripe.native.ts for mobile which exports the actual stripe components/functions. Now I can import by ./stripe and it will automatically import the correct module based on which platform I’m using.

hey guyzz for the stripe issue you can initially add null loader like this to resolve the TextInputState Error here is my webpackConfig.js file for adding alias Screenshot from 2023-11-27 17-36-09 and create a separate NullModuleloader file like this Screenshot from 2023-11-27 17-37-11

I’m facing only this error:

Module not found: Can't resolve './RCTNetworking'
  16 | const EventTarget = require('event-target-shim');
  17 | const GlobalPerformanceLogger = require('../Utilities/GlobalPerformanceLogger');
> 18 | const RCTNetworking = require('./RCTNetworking');
     |                      ^
  19 |
  20 | const base64 = require('base64-js');
  21 | const invariant = require('invariant');

Did you fix it mate ?

@anabeatrizzz

Facing this issue too, the fix posted by @robert-nash / @MehmetKaplan is not working for me.

@olaurendeau I have a workaround for now. I’ve implemented separate application entry points for mobile and web, with a basic dependency injection container to abstract away the platform dependent components. It’s not ideal, but it works.