Webpack Tree-Shaking Breaks Build — Fix (2026)

The Error

TypeError: Cannot read properties of undefined (reading 'create')
  at UserService.init (webpack:///./src/services/user.ts:15:22)
// Module was removed by tree-shaking — the import exists but resolves to undefined

This error occurs in production builds when webpack’s tree-shaking removes a module that your code actually uses. The import appears to succeed but the exported value is undefined.

The Fix

  1. Mark the module as having side effects in package.json:
{
  "sideEffects": [
    "*.css",
    "./src/polyfills.ts",
    "./src/services/init.ts"
  ]
}
  1. Or mark the entire package as not tree-shakeable:
{
  "sideEffects": true
}
  1. Alternatively, use explicit named imports instead of namespace imports:
// Before (tree-shaking may remove):
import * as utils from './utils';
// After (explicit, tree-shaking preserves):
import { createUser, deleteUser } from './utils';
  1. Rebuild and test:
npx webpack --mode production
node dist/main.js

Why This Happens

Webpack’s tree-shaking analyzes static imports to determine which exports are used and removes unused ones. When Claude generates code using dynamic patterns, re-exports, or barrel files, webpack may incorrectly determine that an import is unused. Barrel files (index.ts that re-export everything) are especially problematic because webpack must trace through the entire re-export chain.

If That Doesn’t Work

  • Add a webpack comment to preserve the import:
import(/* webpackExports: ["create"] */ './services/user');
  • Disable tree-shaking for a specific module in webpack config:
module.exports = {
  optimization: {
    usedExports: true,
    sideEffects: true
  }
};
  • Replace barrel files with direct imports from source files.

Prevention

Add this to your CLAUDE.md:

# Webpack Build
- Use named imports, never import * from barrel files.
- Mark files with side effects in package.json sideEffects array.
- Test production builds after every code change: npx webpack --mode production.

See Also

This fix also applies if you see variations of this error:

  • Connection or process errors with similar root causes in the same subsystem
  • Timeout variants where the operation starts but does not complete
  • Permission variants where access is denied to the same resource
  • Configuration variants where the same setting is missing or malformed

If your specific error message differs slightly from the one shown above, the fix is likely the same. The key indicator is the operation that failed (shown in the stack trace) rather than the exact wording of the message.

Frequently Asked Questions

Does this error affect all operating systems?

This error can occur on macOS, Linux, and Windows (WSL). The exact error message may differ slightly between platforms, but the root cause and fix are the same. macOS users may see additional Gatekeeper or notarization prompts. Linux users should check that the relevant system packages are installed. Windows users should ensure they are running inside WSL2, not native Windows.

Will this error come back after updating Claude Code?

Updates can occasionally reintroduce this error if the update changes default configurations or dependency requirements. After updating Claude Code, verify your project still builds and runs correctly. If the error returns, reapply the fix and check the changelog for breaking changes.

Can this error cause data loss?

No, this error occurs before or during an operation and does not corrupt existing files. Claude Code’s edit operations are atomic — they either complete fully or not at all. However, if the error occurs during a multi-step operation, you may have partial changes that need to be reviewed with git diff before continuing.

How do I report this error to Anthropic if the fix does not work?

Open an issue at github.com/anthropics/claude-code with: (1) the full error message including stack trace, (2) your Node.js version (node --version), (3) your Claude Code version (claude --version), (4) your operating system and version, and (5) the command or operation that triggered the error.

Try it: Paste your error into our Error Diagnostic for an instant fix.