Hooks (useConfig, useForm etc.) returning empty objects in Payload pnpm monorepo setup

default discord avatar
58bits
3 weeks ago
2

Hi All,



We're working on an upgrade to Payload 2.0 in a monorepo setup.



Everything went fairly smoothly - we followed the 2.0 release notes here

https://github.com/payloadcms/payload/releases/tag/v2.0.0

and we can build Payload using the webpackBundler fine. The admin dashboard loads fine as well.



However, I think we're still missing something in terms of where Payload is looking for configuration information. We have a few custom components that call

useConfig

useForm

etc., but they're returning empty objects and cause errors when we try to access a collection that has one of our special components in it.



We start payload in dev as follows:


"dev": "PAYLOAD_CONFIG_PATH=../cms/dist/payload.config.js NODE_ENV=development remix dev --manual -c \"node --conditions=serve server.js\" | pino-pretty",

And our custom express server initializes Payload as...



import cms from '@project/cms'
const { payload } = cms
...


...
// Payload section here - top level await since we're now an ESM module
await payload.init({
  secret: PAYLOAD_SECRET,
  express: app,
  onInit: () => {
    payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`)
  }
})
...


We've read in the docs that it may help to pass the Payload config directly to init...

https://github.com/payloadcms/payload/blob/3e5149bc432578b5c547e19eb9ed360d8e6c4626/packages/payload/src/config/types.ts#L115

- would this help? Confess we're not 100% sure how this works.



Feels like we're missing something fundamental when we bootstrap payload. Any thoughts or suggestions greatly appreciated.



If this helps - it's not just our components.

useConfig

is returning an empty object for the new Lexical richtext editor....


map is failing because collections in

const { collections } = useConfig()

is undefined @alessiogr any ideas?

  • discord user avatar
    alessiogr
    Payload Team
    3 weeks ago

    First thing I'd do is to absolutely make sure that you do not have duplicate versions of payload, react or react-dom installed.



    99% of times, if a react hook like that returns undefined, it's because that piece of code is using a different, duplicated version of payload/react/react-dom



    Had a similar problem inside the payload repo too - one thing which helped was switching from yarn monorepos to pnpm monorepos. pnpm seems to handle that better

  • default discord avatar
    58bits
    3 weeks ago

    Thanks @alessiogr - I'll try a few things.



    You were spot on, and we found the duplicate version of Payload - thanks @alessiogr

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.