Skip to content

payload run crashes on Node 24: loadEnvConfig of undefined in loadEnv.ts #16341

@micahshu

Description

@micahshu

Describe the Bug

payload run <script> crashes immediately on Node.js 24 with:

TypeError: Cannot destructure property 'loadEnvConfig' of 'import_env.default' as it is undefined.
at loadedEnvFiles (payload/dist/bin/loadEnv.js:3:9)

Root cause

loadEnv.ts uses a default import from @next/env:

import nextEnvImport from '@next/env'
const { loadEnvConfig } = nextEnvImport

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

Fix

Use a namespace import with a fallback instead of a default import:

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

Named imports (import { loadEnvConfig }) also fail on Node 24. The CJS bundle is not statically analyzable so Node cannot
detect its named exports. The namespace import (import * as) is the only form that works.

Environment

  • Payload: 3.83.0
  • Node: 24.15.0
  • OS: Linux (WSL2)

Link to the code that reproduces this issue

https://github.com/payloadcms/payload/blob/main/packages/payload/src/bin/loadEnv.ts

Reproduction Steps

  1. Create a Payload 3.x + Next.js project (npx create-payload-app)
  2. Create a script at src/scripts/test.ts:
import { getPayload } from 'payload'
import config from '@payload-config'

async function main() {
  const payload = await getPayload({ config })
  console.log('ok')
  await payload.destroy()
}

main()
  1. Run npx payload run src/scripts/test.ts with Node.js 24

Expected: script runs and prints ok
Actual: crashes before the script executes with TypeError: Cannot destructure property 'loadEnvConfig' of 'import_env.default'
as it is undefined

Does not reproduce on Node 18 or 20. only Node 24, which enforces stricter CJS→ESM interop rules.

Which area(s) are affected?

area: core

Environment Info

- Payload: 3.83.0
- Node.js: 24.15.0
- Next.js: 16.2.4
- OS: Linux (WSL2) / Ubuntu

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreCore Payload functionalitystatus: needs-triagePossible bug which hasn't been reproduced yet

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions