AWS Lambda Error: Cannot find module 'stripe' Require stack

AWS Lambda Error: Cannot find module 'stripe' Require stack

我正在尝试将 Stripe 集成到带有 AWS Amplify 后端的 React Native 应用程序中。

我的schema.graphql包括两种

type Mutation {
  createPaymentIntent(amount: Int!): PaymentIntent! @function(name: "createPaymentIntent-${env}")
}

type PaymentIntent {
  clientSecret: String!
}

函数index.js需要带秘钥的stripe

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,
    }
};

在 运行 放大推送后,当我尝试 运行 AWS AppSync 中的这种突变时,我能够 select 突变。

mutation MyMutation {
  createPaymentIntent(amount: 10) {
    clientSecret
  }
}

它 returns 一个错误。

{
  "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"
    }
  ]
}

我已经尝试删除并重新添加该功能。另外我 运行 yarn add stripe inside the function 文件夹。函数文件夹中的 package.json 如下所示:

{
  "name": "createPaymentIntent",
  "version": "2.0.0",
  "description": "Lambda function generated by Amplify",
  "main": "index.js",
  "license": "Apache-2.0",
  "devDependencies": {
    "stripe":"^8.209.0"
  }
}

预期的行为将返回 client_secret。

显然问题出在 package.json。

我设法通过将所有 devDependencies 移动到依赖项来让它工作。我记得以前尝试过这个,但它没有用,因为我用相同的包填充了 devDependencies 和依赖项。工作代码现在看起来像这样:

{
  "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": {}
}