FG
๐Ÿ”Œ APIs & SDKsTwilio

TypeError: Cannot read properties of undefined (reading 'prototype') when using Next.js 16 + Turbopack

Freshabout 16 hours ago
Mar 14, 20260 views
Confidence Score50%
50%

Problem

Description I am encountering a `TypeError` immediately upon starting my Next.js application when `twilio` is imported. This issue appears to be related to how the library is bundled by Turbopack (Next.js bundler) in a dev environment. The error disappears if I add `twilio` to `serverExternalPackages` in `next.config.mjs`, forcing it to be excluded from the bundle. Steps to Reproduce 1. Initialize a Next.js 16 project. 2. Install `twilio`. 3. Import `twilio` in a server-side file (e.g., a service or API route). 4. Run the dev server with Turbopack: `next dev --turbo`. Code Snippet [code block] Exception/Log [code block] OS: macOS Darwin 25.1.0 (arm64) Bundler: Turbopack (`--turbo`) Workaround Adding twilio to serverExternalPackages in next.config.mjs solves the issue: [code block] Environment Node.js: v20.19.5 Next.js: v16.0.1 Twilio: v5.10.6 OS: macOS Darwin 25.1.0 (arm64) * Bundler: Turbopack (--turbo)

Error Output

error disappears if I add `twilio` to `serverExternalPackages` in `next.config.mjs`, forcing it to be excluded from the bundle.

Unverified for your environment

Select your OS to check compatibility.

2 Fixes

Canonical Fix
Moderate Confidence Fix
66% confidence100% success rate1 verificationLast verified Mar 17, 2026

Externalize twilio from Turbopack bundle using serverExternalPackages

Low Risk

Bundling incompatibility between twilio and Turbopack dev bundling path. Twilio expects Node-oriented module evaluation that Turbopack does not preserve correctly.

66

Trust Score

1 verification

100% success
  1. 1

    Add serverExternalPackages to next.config.mjs

    Force twilio out of the Turbopack bundle by adding it to serverExternalPackages.

    code
    const nextConfig = {
      serverExternalPackages: ["twilio"],
    };
    export default nextConfig;
  2. 2

    Restart dev server

    Restart the Next.js dev server to pick up the config change.

  3. 3

    Verify no TypeError

    Confirm the TypeError: Cannot read properties of undefined (reading prototype) no longer appears.

Validation

Externalizing twilio from the Turbopack bundle avoids the crash path and restores local development.

Verification Summary

Worked: 1
Last verified Mar 17, 2026

Sign in to verify this fix

1 low-confidence fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Externalize twilio from Turbopack bundle using serverExternalPackages

Low Risk

Twilio's package expects Node.js-oriented module evaluation that Turbopack does not preserve correctly in the dev bundling path. Turbopack attempts to bundle twilio like a client module, causing prototype chain access to fail on undefined intermediary objects.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Add twilio to serverExternalPackages in next.config

    This tells Next.js/Turbopack to treat twilio as a server-only external, loaded from node_modules at runtime instead of bundled.

    typescript
    // next.config.mjs
    /** @type {import("next").NextConfig} */
    const nextConfig = {
      serverExternalPackages: ["twilio"],
    }
    export default nextConfig
  2. 2

    Ensure twilio is only used in server context

    Move all twilio imports and usage to Server Components, Route Handlers, or Server Actions. Never import twilio in client components or shared utility files that run on both sides.

  3. 3

    Restart dev server

    After changing next.config.mjs, fully restart the Next.js dev server (not just hot reload) for the change to take effect.

Validation

The dev server starts without the TypeError. Twilio API calls succeed in Route Handlers. No prototype-related errors in the console.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

twiliosmsapistale