Load Stripe.js With Require.js
I'm having trouble loading Stripe.js with Require.js. My setup looks a bit like this requirejs.config({ paths: { 'stripe': 'https://js.stripe.com/v3/?noext' }, shim: {
Solution 1:
The global that stripe exports is Stripe
with an uppercase "S". The exports
needs to match the global export exactly, meaning case.
This works:
requirejs.config({
paths: {
'stripe': 'https://js.stripe.com/v3/?noext'
},
shim: {
'stripe': {
exports: 'Stripe'// Uppercase
}
}
});
require(['stripe'], function(s) {
// Based on code from https://stripe.com/docs/stripe-js/elements/quickstartconst ss = s('pk_test_g6do5S237ekq10r65BnxO6S0');
const elements = ss.elements();
const card = elements.create('card');
card.mount('#card-element');
});
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.js"></script><divid="card-element"></div>
This doesn't:
requirejs.config({
paths: {
'stripe': 'https://js.stripe.com/v3/?noext'
},
shim: {
'stripe': {
exports: 'stripe'// Lowercase
}
}
});
require(['stripe'], function(s) {
console.log(s); // undefined
});
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.js"></script>
Post a Comment for "Load Stripe.js With Require.js"