payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
25.73k stars 1.64k forks source link

@payloadcms/next/dist/cjs/withPayload.js has wrong file ending. #8635

Closed ckruppe closed 1 month ago

ckruppe commented 1 month ago

Link to reproduction

No response

Environment Info

Binaries: Node: 20.11.1 npm: N/A Yarn: N/A pnpm: N/A Relevant Packages: payload: 3.0.0-beta.112 next: 15.0.0-canary.173 @payloadcms/db-mongodb: 3.0.0-beta.112 @payloadcms/graphql: 3.0.0-beta.112 @payloadcms/live-preview: 3.0.0-beta.112 @payloadcms/live-preview-react: 3.0.0-beta.112 @payloadcms/next/utilities: 3.0.0-beta.112 @payloadcms/plugin-seo: 3.0.0-beta.112 @payloadcms/richtext-lexical: 3.0.0-beta.112 @payloadcms/richtext-slate: 3.0.0-beta.112 @payloadcms/translations: 3.0.0-beta.112 @payloadcms/ui/shared: 3.0.0-beta.112 react: 19.0.0-rc-3edc000d-20240926 react-dom: 19.0.0-rc-3edc000d-20240926 Operating System: Platform: win32 Arch: x64 Version: Windows 11 Pro Available memory (MB): 32488 Available CPU cores: 16

Describe the Bug

@payloadcms/next exports the withPayload for commonjs wrong. The file in dist/cjs/withPayload.js should have the file ending of cjs as the package.json in @payloadcms/next defines the package type as module.

Here is the node error: Error [ERR_REQUIRE_ESM]: require() of ES Module C:\projekte\Intern\Templates\nextjs-payloadcms-typescript-template\node_modules\@payloadcms\next\dist\cjs\withPayload.js from C:\projekte\Intern\Templates\nextjs-payloadcms-typescript-template\next.config.js not supported. withPayload.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules. Instead either rename withPayload.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in C:\projekte\Intern\Templates\nextjs-payloadcms-typescript-template\node_modules\@payloadcms\next\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

Reproduction Steps

using an commonjs next.config.js file and importing withPayload like so:

const {withPayload} = require('@payloadcms/next/withPayload');

Adapters and Plugins

No response

paulpopus commented 1 month ago

Fixed in https://github.com/payloadcms/payload/pull/8643 and we'll be releasing it shortly!

github-actions[bot] commented 1 month ago

🚀 This is included in version v3.0.0-beta.113

github-actions[bot] commented 1 month ago

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.