Skip to content

payload generate:types fails with ERR_MODULE_NOT_FOUND on extensionless collection imports — tsx tsImport() scoped-registration gap #16734

Open

Description

@Paragon-Dynamics

TL;DR

pnpm payload generate:types fails on the file layout that create-payload-app itself scaffolds (collections in separate files imported extensionless from payload.config.ts). Root cause is in tsx/esm/api's tsImport() — its scoped registration doesn't propagate to nested extensionless imports the way tsx's CLI-style global registration does. bin.js uses tsImport() (line 27–29), so the bug surfaces here.

Versions

Minimal repro

  1. Existing Next.js 16 app, no top-level app/layout.tsx (moved into a route group to satisfy Payload v3's requirement).
  2. pnpm dlx create-payload-app@latest --db postgres --db-connection-string "\$DATABASE_URL" --no-agent — scaffolds src/payload.config.ts + src/collections/{Users,Media}.ts. Imports look like import { Users } from './collections/Users' (extensionless, as the CLI writes them).
  3. Add a third collection (Pages) in a separate file with the same pattern, import it the same way.
  4. Fix the well-known CLI bugs (esbuild placeholder in pnpm-workspace.yaml, missing sharp dep, missing @payload-config tsconfig alias, mongoose-vs-postgres adapter mismatch when internal pnpm add fails on ignored builds — happy to file separately).
  5. pnpm typecheck clean, pnpm dev works, /admin serves correctly, schema pushes to Postgres.
  6. Run pnpm payload generate:types to regenerate types for the new Pages collection.

Expected

src/payload-types.ts regenerates with the Page interface.

Actual

```
Error [ERR_MODULE_NOT_FOUND]: Cannot find module
'/path/to/project/src/collections/Users'
imported from /path/to/project/src/payload.config.ts
at finalizeResolution (node:internal/modules/esm/resolve:275:11)
at moduleResolve (node:internal/modules/esm/resolve:861:10)
at defaultResolve (node:internal/modules/esm/resolve:985:11)
at nextResolve (node:internal/modules/esm/hooks:748:28)
at resolve (file:///path/to/project/node_modules/.pnpm/[email protected]/.../dist/esm/index.mjs:2:5279)
at nextResolve (node:internal/modules/esm/hooks:748:28)
code: 'ERR_MODULE_NOT_FOUND',
url: 'file:///path/to/project/src/collections/Users'
```

tsx's resolve hook IS in the chain, but defers to nextResolve (Node default) without trying .ts/.tsx extensions.

Investigation

The same extensionless import resolves correctly under tsx's CLI-style global registration:

```bash
node --import='tsx/esm' --env-file=.env.local -e \
"import('./src/payload.config.ts').then(c => console.log(typeof c.default))"
```

→ loads payload.config.ts, resolves ./collections/Users, fails downstream on something unrelated (loadEnvConfig from Next.js's env loader — distinct from this issue).

So tsx itself handles extensionless nested imports — but only under global registration. The tsImport() programmatic API used by bin.js:27-29 doesn't propagate resolution to nested imports the same way.

Five other workaround paths tried, each failed differently (architectural-problem signature — stopped at 5 attempts):

  1. --use-swc + @swc-node/[email protected] → SyntaxError: ./collections/Media does not provide an export named 'Media'
  2. Custom wrapper importing payload/dist/bin/generateTypes.js → ERR_PACKAGE_PATH_NOT_EXPORTED (internal not in exports map)
  3. node --import='tsx/esm' node_modules/payload/dist/bin/index.js → silent (only exports bin, never calls it)
  4. node --import='tsx/esm' --env-file=.env.local payload/bin.js generate:types --disable-transpile → ERR_REQUIRE_CYCLE_MODULE on payload.config.ts (CJS↔ESM cycle from project's mixed module system)
  5. Manually adding .js extensions to the imports (canonical Payload v3 ESM pattern) → fixes the CLI but breaks `pnpm dev` with ERR_MODULE_NOT_FOUND (Next.js bundler can't find Users.js since only Users.ts exists)

Suggested fix

In bin.js, switch from tsx/esm/api's tsImport() to Node's module.register('tsx/esm', ...) style global registration before invoking dist/bin/index.js. The CLI/global form is verified to handle nested extensionless imports; only the scoped programmatic form has the gap. Alternative: file upstream at privatenumber/tsx to make tsImport() behaviorally match the CLI for nested resolution.

Workaround in our project

Local typed cast in the consuming Server Component instead of importing the generated Page type — ~16 lines, 2-line semantic loss (no autocomplete on the Pages collection beyond what we declared in the local type).

Happy to provide a more reduced repro repo if useful.

Activity

  1. SakshixSingh commented on Jun 23, 2026

    @SakshixSingh
    Contributor

    Hi team — this is ready for review when you have a moment.
    This PR fixes generate:types failing on extensionless collection imports (the default create-payload-app layout). Relevant unit test is included and passes locally.
    Thanks!

  2. mhornbacher commented on Sep 22, 2026

    @mhornbacher

    What is holding this fix up?

    extension-less imports are quite standard and this is a real blocker for payload adoption?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions