'AWS Lambda Error: Cannot find module 'stripe' Require stack
I am trying to integrate Stripe into a React Native app with an AWS Amplify backend.
My schema.graphql includes two types
type Mutation {
createPaymentIntent(amount: Int!): PaymentIntent! @function(name: "createPaymentIntent-${env}")
}
type PaymentIntent {
clientSecret: String!
}
The index.js of the function requires stripe with a secret key.
const stripe = require('stripe')(<secret.key>)
exports.handler = async (event) => {
const { typeName, arguments } = event;
if (typeName !== 'Mutation') {
throw new Error('Request is not a mutation');
}
if (!arguments?.amount) {
throw new Error('Amount argument is required');
}
// create payment intent
const paymentIntent = await stripe.paymentIntents.create({
amount: arguments.amount,
currency: 'usd'
});
return {
clientSecret: paymentIntent.client_secret,
}
};
After running amplify push, when I try to run this mutation in AWS AppSync I am able to select the mutation.
mutation MyMutation {
createPaymentIntent(amount: 10) {
clientSecret
}
}
It returns an error.
{
"data": null,
"errors": [
{
"path": [
"createPaymentIntent"
],
"data": null,
"errorType": "Lambda:Unhandled",
"errorInfo": null,
"locations": [
{
"line": 2,
"column": 3,
"sourceName": null
}
],
"message": "Error: Cannot find module 'stripe'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js"
}
]
}
I already tried to remove and re-add the function. Also I ran yarn add stripe inside the function folder. My package.json in the function folder looks like this:
{
"name": "createPaymentIntent",
"version": "2.0.0",
"description": "Lambda function generated by Amplify",
"main": "index.js",
"license": "Apache-2.0",
"devDependencies": {
"stripe":"^8.209.0"
}
}
The expected behavior would be returning a client_secret.
Solution 1:[1]
Apparently the problem was the package.json.
I managed to get it working by moving all devDependencies to dependencies. I remember trying this before but it didn't work since I populated both devDependencies and dependencies with the same packages. Working code looks like this now:
{
"name": "createPaymentIntent",
"version": "2.0.0",
"description": "Lambda function generated by Amplify",
"main": "index.js",
"license": "Apache-2.0",
"dependencies": {
"@stripe/stripe-react-native": "^0.4.0",
"stripe": "^8.209.0"
},
"devDependencies": {}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user13607508 |
