TypeError: Cannot read properties of undefined (reading 'prototype') when using Next.js 16 + Turbopack
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
Externalize twilio from Turbopack bundle using serverExternalPackages
Bundling incompatibility between twilio and Turbopack dev bundling path. Twilio expects Node-oriented module evaluation that Turbopack does not preserve correctly.
Trust Score
1 verification
- 1
Add serverExternalPackages to next.config.mjs
Force twilio out of the Turbopack bundle by adding it to serverExternalPackages.
codeconst nextConfig = { serverExternalPackages: ["twilio"], }; export default nextConfig; - 2
Restart dev server
Restart the Next.js dev server to pick up the config change.
- 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
Sign in to verify this fix
1 low-confidence fix
Externalize twilio from Turbopack bundle using serverExternalPackages
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
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
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
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
Alex Chen
2450 rep