---
name: tsx-node22-esm-named-exports
description: tsx@4.21.0 with Node.js v22 and --import tsx/esm fails for named exports from CJS packages — use --use-swc flag or import map workaround
type: concept
---

# tsx + Node.js v22 — ESM Named Exports from CJS Packages Fail

## The Error

Running a TypeScript file with `tsx` on Node.js v22:

```bash
node --import tsx/esm script.ts
```

Fails with:

```
SyntaxError: The requested module 'some-cjs-package' does not provide an export named 'SomeName'
```

Or:

```
Error [ERR_PACKAGE_IMPORT_NOT_DEFINED]: Package import specifier "some-package" is not defined in package...
```

## Root Cause

Node.js v22 tightened ESM/CJS interop rules. When `tsx/esm` loader is used, Node treats `.js` imports as ESM if the package has `"type": "module"` — but many packages that export named exports are actually CJS bundles that Node v22 refuses to re-export as named ESM exports.

## Workarounds

### Option 1: `--use-swc` flag (recommended)

```bash
node --import tsx/esm --use-swc script.ts
```

`--use-swc` tells tsx to use the SWC compiler which handles the CJS↔ESM boundary differently.

### Option 2: Direct `tsx` invocation

```bash
npx tsx script.ts
# Instead of: node --import tsx/esm script.ts
```

`tsx` CLI handles the loader registration itself with better Node v22 compat.

### Option 3: Import Map

Create `importMap.js` that re-exports the CJS package as a proper ESM shim:

```js
// importMap.js
export { SomeName } from 'some-cjs-package'
```

Then import from the shim instead of the original package.

## Affected Pattern

This primarily affects:
- Payload CMS seed scripts
- Any TypeScript scripts using `--import tsx/esm` (as opposed to the `tsx` CLI)
- Packages with `"exports"` in `package.json` that conditionally expose ESM/CJS

## Node Version Check

```bash
node --version  # v22.x triggers this
```

If you must stay on Node v22, prefer `npx tsx` over `node --import tsx/esm`.

**Source:** daily/2026-05-13.md | 2026-05-13
