Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Error: missing secret key. A secret key is needed to secure Payload.

default discord avatar
creonicolast year
7

This was working yesterday in sveltekit, but today if fails with this error after I npm run dev:


Error: missing secret key. A secret key is needed to secure Payload.

But my config has the secret PAYLOAD_SECRET property from my .env file:


import path from 'path'
import { buildConfig } from 'payload'
import { fileURLToPath } from 'url'
import { postgresAdapter } from '@payloadcms/db-postgres'

import { Products } from './collections/Products'

const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)

export default buildConfig({
  collections: [Products],
  secret: process.env.PAYLOAD_SECRET || '',
  typescript: {
    outputFile: path.resolve(dirname, 'payload-types.ts'),
  },
  db: postgresAdapter({
    pool: {
      connectionString: process.env.DATABASE_URI || '',
    },
  })
})

I then use the local API as such, this is where the error comes from but it was working yesterday


import config from '../../payload.config'
import { getPayload } from 'payload';

const payload = await getPayload({ config });

export async function load() {
  const data = await payload.findByID({
    collection: 'products',
    id: 1
  });

  return data;
}

I have not changed my package.json at all, in fact, I only ran the command since yesterday and now it gives me this error. I am using payload 3.0.0-beta.74

  • default discord avatar
    notchrlast year
    @254395642565099530

    Is your .env being picked up correctly?



    For instance, can you log the value of your process.env



    And when doing so, is it an object with the expected variables



    Try logging it prior to the buildConfig

  • default discord avatar
    creonicolast year

    It prints but it does not contain the variables in my .env

  • default discord avatar
    notchrlast year

    Ah



    Okay lets try to fix that



    can you try to add



    import 'dotenv/config'


    at the top



    Then try logging again

  • default discord avatar
    creonicolast year

    it works now, thank you. But may I know why it failed before?



    It was working yesterday without dotenv/config

  • default discord avatar
    notchrlast year

    Hmm



    Good question, not the first time I've seen it too



    And im not sure, typically environment variables are only added if they are declared as part of a command or manually declared



    Otherwise they are pulled in via an .env file and the dotenv library



    That's the extent of my knowledge though, not sure if there's some process that pulls it in the first time



    If you do find out, let me know for sure, im glad it works though 😄

  • default discord avatar
    creonicolast year

    Alright, thank you again

  • default discord avatar
    notchrlast year

    Anytime!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.