FG
๐Ÿ”Œ APIs & SDKsStripe

stripe__WEBPACK_IMPORTED_MODULE_0__.default is not a constructor

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score51%
51%

Problem

Describe the bug I receive an error when importing stripe: I'm using next 13 with app dir and latest version of stripe. To Reproduce 1. Create a next 13 app. 2. Create a `stripe.ts` file to initialize the library. 3. Try to use it in a server route. Expected behavior The library should return the expected Stripe object since it's not marking any apparent error. Code snippets _No response_ OS Windows 11 Node version Node v18.16.0 Library version "stripe": "12.14.0" API version 2022-11-15 Additional context _No response_

Error Output

error when importing stripe:

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Fix Stripe Import Error in Next.js 13 App

Medium Risk

The error occurs because the Stripe library is being imported incorrectly. In the latest versions of the Stripe library, the default export is not a constructor, which leads to the 'not a constructor' error when trying to instantiate it. This is likely due to changes in how the library is structured and exported.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Update Stripe Initialization

    Modify the way you import and initialize the Stripe library in your `stripe.ts` file. Use named imports instead of default imports.

    typescript
    import { Stripe } from 'stripe';
    
    const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, { apiVersion: '2022-11-15' });
  2. 2

    Check Environment Variables

    Ensure that your Stripe secret key is correctly set in your environment variables. This is critical for the Stripe instance to work properly.

    typescript
    console.log(process.env.STRIPE_SECRET_KEY); // Should log your secret key
  3. 3

    Use Stripe in Server Route

    When using the Stripe instance in your server route, ensure that you are correctly importing the initialized Stripe object from your `stripe.ts` file.

    typescript
    import stripe from './stripe';
    
    export default async function handler(req, res) {
      // Use the stripe instance here
    }
  4. 4

    Test the Integration

    Run your Next.js application and test the endpoint that uses the Stripe instance to confirm that the error is resolved and the Stripe API works as expected.

    bash
    curl -X POST http://localhost:3000/api/your-stripe-endpoint

Validation

Confirm that the error no longer appears when you run your application and that you can successfully interact with the Stripe API without issues.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

stripepaymentsapibug