fix(next): use namespace import for @next/env to fix Node 24 ESM compat by micahshu · Pull Request #16342 · payloadcms/payload · GitHub
Skip to content

fix(next): use namespace import for @next/env to fix Node 24 ESM compat#16342

Open
micahshu wants to merge 1 commit intopayloadcms:mainfrom
micahshu:fix/load-env-node24-esm-compat
Open

fix(next): use namespace import for @next/env to fix Node 24 ESM compat#16342
micahshu wants to merge 1 commit intopayloadcms:mainfrom
micahshu:fix/load-env-node24-esm-compat

Conversation

@micahshu
Copy link
Copy Markdown

What

Fixes payload run crashing immediately on Node.js 24 with:

  TypeError: Cannot destructure property 'loadEnvConfig' of 'import_env.default' as it is undefined.

Why

@next/env is a CJS-only bundle that sets __esModule: true on module.exports but has no .default property. Node 24
strictly follows the __esModule flag and returns undefined for default imports. Older Node versions (18/20) fall back to
the whole module.exports object, which is why this only surfaces on Node 24.

Named imports also fail — the CJS bundle is not statically analyzable so Node 24 cannot detect its named exports. The namespace
import (import * as) is the only form that works across all Node versions.

How

- import nextEnvImport from '@next/env'
- const { loadEnvConfig } = nextEnvImport
+ import * as nextEnvImport from '@next/env'
+ const loadEnvConfig = nextEnvImport.loadEnvConfig ?? (nextEnvImport as any).default?.loadEnvConfig

Closes #16341

…ompat

Node 24 strictly follows the __esModule flag on CJS modules and returns
undefined for default imports when no .default property exists. @next/env
is a CJS-only bundle that sets __esModule: true but has no default export,
causing `payload run` to crash immediately on Node 24.

Switching to a namespace import with a fallback resolves the crash on Node
24 while remaining compatible with older Node versions (18/20).

Fixes payloadcms#16341
@micahshu micahshu changed the title fix(loadEnv): use namespace import for @next/env to fix Node 24 ESM compat fix(payload): use namespace import for @next/env to fix Node 24 ESM compat Apr 22, 2026
@micahshu micahshu changed the title fix(payload): use namespace import for @next/env to fix Node 24 ESM compat fix(payload): use namespace import for @next/env to fix Node 24 ESM compat Apr 22, 2026
@micahshu micahshu changed the title fix(payload): use namespace import for @next/env to fix Node 24 ESM compat fix(next): use namespace import for @next/env to fix Node 24 ESM compat Apr 22, 2026
@denolfe denolfe added the v3 label Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants