Bug: payload generate:types fails on Windows (module resolution / ESM loader issues)
Describe the Bug
Bug: payload generate:types fails on Windows (module resolution / ESM loader issues)
Environment
- OS: Windows 11
- Shell: PowerShell
- Node: v22.19.0
- Package managers tested:
- Bun
- npm / npx
- Framework: Next.js + Payload
- Project created using
create-payload-app
Summary
Running:
payload generate:typesfails on Windows, even in a freshly scaffolded Payload project before adding custom code.
I encountered two related issues depending on import style in payload.config.ts.
Reproduction
1. Create a fresh project
npx create-payload-app@latestOr clone this repo
https://github.com/ahmadmh404/nextjs-16.2-payload-3.84-bug-reproduced-enviroment.git2. Run type generation
Using Bun:
bun run generate:typesor directly:
npx payload generate:typesIssue A — Module not found with extensionless imports
Using default-style imports in payload.config.ts:
import { Users } from "./collections/Users";produces:
Error [ERR_MODULE_NOT_FOUND]:
Cannot find module
.../src/collections/Users
imported from .../src/payload.config.tsExample full error:
Error [ERR_MODULE_NOT_FOUND]: Cannot find module
'E:\Code\projects\multi-tenant-ecommerce\src\collections\Users'
imported from
E:\Code\projects\multi-tenant-ecommerce\src\payload.config.tsThe file exists:
src/collections/Users.ts- File exists
- Casing is correct
- Reproduces in fresh generated project
Issue B — Adding .ts extensions changes error
Changing imports to:
import { Users } from "./collections/Users.ts";and enabling in tsconfig.json:
"allowImportingTsExtensions": truegets past the module resolution issue, but then type generation fails with:
Error [ERR_REQUIRE_ASYNC_MODULE]:
require() cannot be used on an ESM graph with top-level awaitFull error:
Error [ERR_REQUIRE_ASYNC_MODULE]:
require() cannot be used on an ESM graph with top-level await.
Use import() instead.
Requiring .../src/payload.config.tsEven though payload.config.ts contains no top-level await.
Minimal Config Used
Fresh generated config (no custom async logic added):
import { mongooseAdapter } from "@payloadcms/db-mongodb";
import { lexicalEditor } from "@payloadcms/richtext-lexical";
import path from "path";
import { buildConfig } from "payload";
import { fileURLToPath } from "url";
import sharp from "sharp";
import { Users } from "./collections/Users";
import { Media } from "./collections/Media";
import { Categories } from "./collections/Categories";
const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
export default buildConfig({
admin: {
user: Users.slug,
importMap: {
baseDir: path.resolve(dirname),
autoGenerate: true,
},
},
collections: [Users, Media, Categories],
editor: lexicalEditor(),
secret: process.env.PAYLOAD_SECRET || "",
typescript: {
outputFile: path.resolve(dirname, "payload-types.ts"),
},
db: mongooseAdapter({
url: process.env.DATABASE_URL || "",
}),
sharp,
plugins: [],
});No top-level await anywhere.
Expected Behavior
Running:
payload generate:typesshould generate:
payload-types.tssuccessfully on Windows.
Actual Behavior
Type generation fails:
Extensionless imports:
ERR_MODULE_NOT_FOUND
.tsimports:ERR_REQUIRE_ASYNC_MODULE
Both block type generation.
Additional Notes
- Reproduces on fresh generated project
- Reproduces with:
- Bun
npx payload generate:types
Appears related to one or both of:
- Windows ESM resolution for extensionless TypeScript imports
- Payload CLI config loading (
require()vs ESM import) tsxloader behavior on Windows- Possible Node 22 compatibility issue
Reproduction Repository
Minimal repro:
https://github.com/ahmadmh404/nextjs-16.2-payload-3.84-bug-reproduced-enviroment.gitPossible Areas to Investigate
- Windows ESM resolution of extensionless
.tsimports - Payload CLI loading config through
require() tsxloader interaction on Windows- Node 22 compatibility
- Bun interaction (possibly secondary)
Happy to help test patches or provide more debugging output if needed.
Link to the code that reproduces this issue
https://github.com/ahmadmh404/nextjs-16.2-payload-3.84-bug-reproduced-enviroment.gitReproduction Steps
- Clone the reproduction repository:
git clone https://github.com/ahmadmh404/nextjs-16.2-payload-3.84-bug-reproduced-enviroment.git
cd nextjs-16.2-payload-3.84-bug-reproduced-enviroment- Install dependencies:
Using npm:
npm installor using Bun:
bun install- Run type generation:
npx payload generate:typesor:
bun run generate:typesWhich area(s) are affected?
area: core
Environment Info
Binaries:
Node: 22.19.0
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant Packages:
payload: 3.84.1
next: 16.2.4
@payloadcms/db-mongodb: 3.84.1
@payloadcms/graphql: 3.84.1
@payloadcms/next/utilities: 3.84.1
@payloadcms/richtext-lexical: 3.84.1
@payloadcms/translations: 3.84.1
@payloadcms/ui/shared: 3.84.1
react: 19.2.4
react-dom: 19.2.4
Operating System:
Platform: win32
Arch: x64
Version: Windows 10 Pro
Available memory (MB): 12195
Available CPU cores: 8Source: payloadcms/payload