# Fix date-fns locale import failing and breaking tree shaking

## TL;DR

You imported the locale from the package root, so import it from the locale subpath instead. Deep imports let the bundler shake off the locales you do not use. Importing from the root pulls every locale into the bundle and can break the named import entirely.

## The error

```text
Module not found or bundle includes all 60+ locales
import { es } from 'date-fns' -- locale undefined or bundle bloat
```

## Fix it

### Step 1: Check the current import

```bash
grep -rn "from 'date-fns'" src/ | grep -i "locale" | head -5
```

Expected: You see the root imports to fix.

### Step 2: Switch to the locale subpath import

```bash
node -e "console.log('use: import { es } from \"date-fns/locale\" -- the documented locale entry point')"
```

Expected: Imports point at the locale entry.

### Step 3: Rebuild and check the bundle for locale bloat

```bash
npx webpack --mode production | tail -3
```

Expected: Only the imported locales appear in the output.

### Step 4: Verify formatting still works

```bash
node -e "console.log('format a date with locale: es, confirm Spanish month names')"
```

Expected: Dates format in the right language.

## When to use this

- date-fns locale import fails or the bundle includes every locale
- You import locales from the package root

## When NOT to use this

- The locale imports but formats wrong, check the locale object itself
- You use date-fns v3 ESM, the subpaths changed, check the v3 docs

## Tool and version compatibility

- date-fns v2, bundlers with tree shaking
- date-fns/locale subpath entry

## Variant phrasings

### v3 ESM import paths

v3 reorganized exports. Check the version's docs for the current locale path, the principle is the same: import from the locale entry.

### locale undefined at runtime

The named import did not match an export. The locale entry exports each locale by name, verify the name.

## Why it happens

date-fns ships each locale as a separate module under the locale entry point. Root imports either miss the export or defeat tree shaking by pulling the index that references everything. The subpath import is the supported contract.

## Edge cases

- Some bundlers need the file extension in deep imports, check yours
- Dynamic locale loading needs import() with the locale path pattern, keep it analyzable
- Pin the date-fns version, locale paths shift between majors

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_P3kCk_str4d8GKVrau2PVw
