amazon-web-servicesstripe-paymentswebhooks

Next.js API Route Error: "Cannot destructure property 'environmentVariableSelector' of 'undefined'" in Serverless Function


I'm encountering a runtime error in a Next.js (v13+) API route that handles Stripe webhooks. The error occurs during the build/deployment process in AWS Amplify, and I can't pinpoint the exact cause.

TypeError: Cannot destructure property 'environmentVariableSelector' of 'undefined' as it is undefined.
at d (.next/server/chunks/5936.js:15:31351)
at eg (.next/server/chunks/5936.js:9:5315)
at new ex (.next/server/chunks/5936.js:9:6739)
at 11151 (.next/server/app/api/stripe/webhook/route.js:1:4571)

Here's a simplified version of my Stripe webhook handler (app/api/stripe/webhook/route.js):

import { NextResponse } from "next/server";
import Stripe from "stripe";
import { updateUserGroupStripe } from "@/lib/actions/admin.action";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);

export async function POST(req) {
  try {
    const text = await req.text();
    const sig = req.headers.get("stripe-signature");
    
    const event = stripe.webhooks.constructEvent(
      text,
      sig,
      process.env.STRIPE_WEBHOOK_SECRET
    );

    await updateUserGroupStripe(payload);

    return NextResponse.json({ received: true });
  } catch (err) {
    console.error(`❌ Error:`, err.message);
    return NextResponse.json({ error: err.message }, { status: 400 });
  }
}

The imported function updateUserGroupStripe lives in a server-side module and interacts with AWS Cognito using the AWS SDK v3.

The function (simplified for clarity):

"use server";
import {
  CognitoIdentityProviderClient,
  AdminAddUserToGroupCommand,
  AdminRemoveUserFromGroupCommand,
  AdminUpdateUserAttributesCommand,
} from "@aws-sdk/client-cognito-identity-provider";

const client = new CognitoIdentityProviderClient({
  region: process.env.COGNITO_REGION,
  credentials: {
    accessKeyId: process.env.COGNITO_ACCESS_KEY_ID,
    secretAccessKey: process.env.COGNITO_ACCESS_SECRET,
  },
});

export async function updateUserGroup({
  username,
  subscription_id,
  subscription_plan_id,
  subscription_next_billing_time,
  action,
}) {
  // Logic omitted for brevity
  const updateAttributesCommand = new AdminUpdateUserAttributesCommand({
    UserPoolId: userPoolId,
    Username: username,
    UserAttributes: attributesToUpdate,
  });
  await client.send(updateAttributesCommand);

}

export async function updateUserGroupStripe({
  username,
  subscription_id,
  subscription_plan_id,
  subscription_next_billing_time,
  customer_id,
  action,
  group,
}) {
  // Logic omitted for brevity
  const updateAttributesCommand = new AdminUpdateUserAttributesCommand({
    UserPoolId: userPoolId,
    Username: username,
    UserAttributes: attributesToUpdate,
  });
  await client.send(updateAttributesCommand);

}

Solution

  • Solved by using this version of SDK:

    "@aws-sdk/client-cognito-identity-provider": "<=3.621.0",
    "@aws-sdk/client-ses": "<=3.621.0",
    

    Reference to the GitHub Post Issue: https://github.com/aws/aws-sdk-js-v3/issues/7051