Stripe in SvelteKit endpoint throws build error on Pages

After a large update of my site, I pushed a new change to Cloudflare and Pages began the build command of my SvelteKit site.

It wasn’t long before I get a fleury of errors all relating to the Stripe-js package, which in short looked like this:

Error: Build failed with 6 errors:
11:48:12.789	node_modules/stripe/lib/crypto/NodeCryptoProvider.js:3:23: ERROR: Could not resolve "crypto"
11:48:12.789	node_modules/stripe/lib/net/NodeHttpClient.js:3:21: ERROR: Could not resolve "http"
11:48:12.789	node_modules/stripe/lib/net/NodeHttpClient.js:4:22: ERROR: Could not resolve "https"
11:48:12.789	node_modules/stripe/lib/stripe.js:48:29: ERROR: Could not resolve "events"
11:48:12.790	node_modules/stripe/lib/utils.js:3:29: ERROR: Could not resolve "events"

It seems like there is support for Stripe in Cloudflare pages + workers and my code is similar to other examples I’ve seen, however this isn’t working. Any help is appreciated!

1 Like

I’m running into the same issue, did you solve this problem?

Same here …

I am also looking for a solution to this issue.

I have also tried every vitejs configuration under the sun to alias http/https/crypto, none of them seem to have any effect on the build process

import polyfillNode from 'rollup-plugin-polyfill-node';
import alias from 'esbuild-plugin-alias';

const config: UserConfig = {
	plugins: [sveltekit(), polyfillNode()],
	resolve: {
		alias: {
			http: false,
			https: false,
			crypto: 'crypto-browserify',
			events: 'events/'
		}
	},
	build: {
		commonjsOptions: {
			include: [/node_modules/, /stripe/]
		}
	},
	ssr: {
		external: ['http', 'https']
	},
	optimizeDeps: {
		include: ['stripe'],
		force: true,
		esbuildOptions: {
			plugins: [
				alias({
					http: false,
					https: false,
					crypto: 'crypto-browserify'
				})
			]
		}
	}
};