code-inspector icon indicating copy to clipboard operation
code-inspector copied to clipboard

ReferenceError: exports is not defined with Turbopack on Next.js 16.0.4

Open AndyMik90 opened this issue 1 month ago • 1 comments

Issue Description When using code-inspector-plugin with Turbopack on Next.js 16.0.4, I intermittently get a ReferenceError: exports is not defined error during HMR (Hot Module Replacement) / Fast Refresh. The error causes the page to crash and display an error boundary.

Environment code-inspector-plugin version: 1.3.0 Next.js version: 16.0.4 Turbopack: Enabled (default in Next.js 16) Node.js version: (run node -v to fill in) OS: macOS

Configuration // next.config.js const { codeInspectorPlugin } = require('code-inspector-plugin')

/** @type {import('next').NextConfig} */ const nextConfig = { turbopack: { rules: process.env.NODE_ENV === 'development' ? codeInspectorPlugin({ bundler: 'turbopack' }) : {}, },

// ... other config } module.exports = nextConfig // next.config.jsconst { codeInspectorPlugin } = require('code-inspector-plugin')/** @type {import('next').NextConfig} */const nextConfig = { turbopack: { rules: process.env.NODE_ENV === 'development' ? codeInspectorPlugin({ bundler: 'turbopack' }) : {}, }, // ... other config}module.exports = nextConfig

Error Message ReferenceError: exports is not defined at append-code-5678.js:1:1416 at append-code-5678.js:1:1498 at module evaluation (append-code-5678.js:1:85445) at dev-base.ts:244:7 at runModuleExecutionHooks (dev-base.ts:278:7) at instantiateModule (dev-base.ts:238:5) at getOrInstantiateModuleFromParent (dev-base.ts:162:10) at DevContext.esmImport [as i] (runtime-utils.ts:347:18) at module evaluation (filter.tsx:1:1) ... at module evaluation (Apps.tsx:52:1) at module evaluation (page.tsx:2:1)

ReferenceError: exports is not defined at append-code-5678.js:1:1416 at append-code-5678.js:1:1498 at module evaluation (append-code-5678.js:1:85445) at dev-base.ts:244:7 at runModuleExecutionHooks (dev-base.ts:278:7) at instantiateModule (dev-base.ts:238:5) at getOrInstantiateModuleFromParent (dev-base.ts:162:10) at DevContext.esmImport [as i] (runtime-utils.ts:347:18) at module evaluation (filter.tsx:1:1) ... at module evaluation (Apps.tsx:52:1) at module evaluation (page.tsx:2:1)

Steps to Reproduce

  1. Set up a Next.js 16.0.4 project with Turbopack (enabled by default)
  2. Configure code-inspector-plugin for Turbopack as per documentation
  3. Run npm run dev (or pnpm dev)
  4. Navigate to any page with components
  5. Make changes to trigger HMR/Fast Refresh
  6. Error occurs intermittently (not on every refresh)

Expected Behavior The plugin should work without errors during development with Turbopack, as documented for Next.js 15.0.3+.

Actual Behavior The error exports is not defined is thrown because Turbopack uses ESM-only modules, but the injected code from the plugin appears to use CommonJS exports syntax.

Analysis The error exports is not defined at append-code-5678.js (a dynamically generated bundle by Turbopack) suggests that the plugin is injecting code that uses CommonJS exports syntax. However, Turbopack operates in an ESM-only runtime and doesn't define the exports global, causing this runtime error.

AndyMik90 avatar Nov 30 '25 18:11 AndyMik90

I can't reproduce this problem. Could you please provide a repo that can reproduce this problem?

zh-lx avatar Dec 01 '25 15:12 zh-lx