How to Fix: NextJs+Turbo+ClerkJs+sentry+HMR causes Module was instantiated … but … is not available. It might have been deleted in an HMR update.
This error is a classic Hot Module Replacement failure in a highly dynamic Next.js development stack: a module was loaded, the dev server pushed an update, and another part of the app still tried to read an export that no longer existed in the refreshed module graph. In the reproduction combining Next.js, Turbopack, Clerk, and Sentry, the bug usually appears because multiple runtime wrappers and client/server boundaries amplify how HMR invalidates modules during development.
Table of Contents
Understanding the Root Cause
The message "Module was instantiated … but … is not available. It might have been deleted in an HMR update" usually means the development bundler rebuilt part of the dependency graph, but another module still references an older version of that graph. This is not the same as a normal import typo. It is a runtime module graph inconsistency during development.
In this setup, several factors can trigger it:
- Turbopack HMR is more aggressive and incremental than a full restart, so stale references are more visible.
- Clerk adds provider wrappers, authentication helpers, and client/runtime dependencies that cross server and client boundaries.
- Sentry often wraps configuration, instrumentation, and app bootstrapping, which adds more indirection to module resolution.
- Next.js App Router mixes server components, client components, layouts, and instrumentation files. If one module changes shape during HMR, another module may still expect the previous export.
Technically, the root problem is usually one of these:
- A client component import path or export shape changes during HMR, but an upstream wrapper still points to the old export.
- A provider such as ClerkProvider is composed in a layout that also depends on other HMR-sensitive wrappers like Sentry-instrumented modules.
- A module that should be stable across refreshes is being recreated through re-export chains, barrel files, or conditional imports.
- Turbopack hits an edge case in dev mode where a deleted or renamed export is not fully invalidated everywhere.
In other words, the issue is less about Clerk or Sentry being "wrong" and more about the interaction between development-time module invalidation, provider composition, and mixed runtime boundaries.
Step-by-Step Solution
The most reliable fix is to simplify the module graph around your app shell, isolate providers, and make sure client-only code stays in stable client modules.
1. Put Clerk in a dedicated client providers file
If you are mounting Clerk directly in a layout with other wrappers, move it into a minimal client component.
'use client'
import { ClerkProvider } from '@clerk/nextjs'
import { ReactNode } from 'react'
export function Providers({ children }: { children: ReactNode }) {
return <ClerkProvider>{children}</ClerkProvider>
}
Then use that provider from your root layout:
import { Providers } from './providers'
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<Providers>{children}</Providers>
</body>
</html>
)
}
This reduces the chance that HMR has to reconcile multiple changing wrappers in the same module.
2. Avoid barrel exports for providers and config during debugging
If you have an index.ts file re-exporting providers, auth utilities, Sentry helpers, or instrumentation code, import modules directly instead of through a barrel.
Prefer:
import { Providers } from './providers'
Instead of:
import { Providers } from './lib'
Barrel files are convenient, but they make HMR invalidation paths harder to track.
3. Keep Sentry instrumentation separate from app-level providers
If Sentry setup is imported into UI composition files, split it out. Your UI tree should not depend on instrumentation modules unless absolutely necessary.
Avoid patterns like:
import './sentry-init'
import { ClerkProvider } from '@clerk/nextjs'
export default function Providers({ children }) {
return <ClerkProvider>{children}</ClerkProvider>
}
Instead, follow the official Sentry file placement for Next.js, keeping instrumentation in dedicated files such as instrumentation.ts, sentry.client.config, or sentry.server.config depending on your integration version.
4. Verify client/server boundaries
Make sure any file importing @clerk/nextjs components that require the browser is marked with ‘use client’ when needed.
'use client'
import { UserButton } from '@clerk/nextjs'
export function HeaderAuth() {
return <UserButton />
}
Do not accidentally import this component into a server-only utility module or mix it with server actions in the same file.
5. Remove unstable conditional exports during development
If a module exports different things based on environment checks, HMR can become inconsistent.
Avoid:
export const authProvider = process.env.NODE_ENV === 'development'
? DevProvider
: ProdProvider
Use stable exports and move conditions inside implementation bodies where possible.
6. Clear the build cache and restart dev server
Once the graph is inconsistent, the fastest reset is a clean restart.
rm -rf .next
pnpm dev
Or with other package managers:
rm -rf .next
npm run dev
This does not fix the structural cause, but it confirms you are dealing with HMR state corruption rather than a permanent import bug.
7. Test without Turbopack
This is the key isolation step. If the error disappears when not using Turbopack, you are likely hitting a dev bundler edge case rather than a production code defect.
next dev
Or explicitly avoid the Turbo flag in your dev script if you added one.
If the bug only happens with Turbopack, practical options are:
- Continue development with the classic Next.js dev bundler until the underlying issue is fixed.
- Keep Clerk and Sentry integrations, but simplify provider composition as shown above.
- Watch upstream fixes in the linked reproduction repository and related framework issue trackers.
8. Pin or align package versions
Cross-package development issues are often version-sensitive. Make sure next, react, react-dom, @clerk/nextjs, and @sentry/nextjs are on compatible versions.
pnpm list next react react-dom @clerk/nextjs @sentry/nextjs
Then update or pin as needed:
pnpm add next@latest react@latest react-dom@latest
pnpm add @clerk/nextjs@latest @sentry/nextjs@latest
If the issue started after an upgrade, test the previous known-good version set.
9. Recommended stable project structure
app/
layout.tsx
providers.tsx
components/
header-auth.tsx
instrumentation.ts
sentry.client.config.ts
sentry.server.config.ts
This structure keeps providers, UI components, and instrumentation separated so HMR can invalidate them more predictably.
10. Final working pattern
For most projects affected by this bug, the combination below is the safest:
- Use a small dedicated client providers file.
- Do not import Sentry initialization into React provider modules.
- Avoid barrel exports for root wiring.
- Restart after large refactors.
- Disable Turbopack temporarily if the issue persists only in dev mode.
Common Edge Cases
- Editing root layout frequently: The root layout is one of the most HMR-sensitive files in App Router. Moving auth and instrumentation out of it helps.
- Mixing server and client imports: Importing Clerk UI components into a server module can cause confusing secondary HMR failures.
- Barrel file re-exports: Re-exporting providers and helpers through a shared index file can preserve stale export expectations after a hot update.
- Sentry auto-instrumentation changes: If you edit instrumentation-related files while the dev server is running, a restart is often required.
- Version drift: A newer Next.js can expose a bundler edge case with older Clerk or Sentry packages, even when the code previously worked.
- False assumption that production is broken: This specific message is commonly a development-only HMR issue. Always verify with a fresh build before treating it as a production failure.
FAQ
Is this a Clerk bug or a Sentry bug?
Usually not directly. It is most often a module invalidation problem in the development stack, triggered by how Clerk, Sentry, and Next.js wrappers interact under HMR.
Why does a full restart fix it temporarily?
A restart rebuilds the entire module graph from scratch, removing stale references created during hot updates. That is why the error often disappears until the next problematic file edit.
Should I disable Turbopack permanently?
Not necessarily. First, isolate providers, remove barrel exports, and separate instrumentation from UI wiring. If the issue still occurs only with Turbopack, using the standard dev server is a reasonable temporary workaround while waiting for an upstream fix.
The practical takeaway is simple: make your root module graph as stable as possible. With Next.js App Router, Clerk, and Sentry, the safest path is to isolate providers, keep instrumentation separate, minimize re-export indirection, and treat Turbopack-only failures as likely development bundler edge cases rather than application logic errors.