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

default discord avatar
taun2160
7 months ago
8

Hi. I don't know why it's returning this error as I have a Payload secret key, similar to this?



dsadadafsdfg-dhsuijhdisa7-djhsjdhsj-hjdhsjddfsdf-uidskjdkadkask



PS C:\git\WhitePolaroids1> yarn dev
yarn run v1.22.19
$ ts-node server.ts
[08:29:55] INFO (payload): Starting Payload...
unhandledRejection Error: Error: missing secret key. A secret key is needed to secure Payload.
    at BasePayload.init (C:\git\WhitePolaroids1\node_modules\payload\src\payload.ts:154:13)
    at getPayload (C:\git\WhitePolaroids1\node_modules\payload\src\payload.ts:407:56)
    at initHTTP (C:\git\WhitePolaroids1\node_modules\payload\src\initHTTP.ts:25:35)
    at Payload.init (C:\git\WhitePolaroids1\node_modules\payload\src\index.ts:14:35)
    at C:\git\WhitePolaroids1\server.ts:14:17
    at step (C:\git\WhitePolaroids1\server.ts:123:23)
    at Object.next (C:\git\WhitePolaroids1\server.ts:64:20)
    at asyncGeneratorStep (C:\git\WhitePolaroids1\server.ts:13:28)
    at _next (C:\git\WhitePolaroids1\server.ts:31:17)
    at C:\git\WhitePolaroids1\server.ts:36:13


I'm using Nextjs custom-server



I think it's working now. My server.ts and .env file had different strings.

  • default discord avatar
    dengusdanny
    5 months ago

    I had the same error and was being caused by using an .env.local file instead of a plain .env file.

  • default discord avatar
    diasmannheim
    4 weeks ago

    is there a solution for this?

  • default discord avatar
    craigb1988
    3 weeks ago

    You figure this out? I've got same issue deploying to Railway, even though all the relevant ENV vars are set.

  • default discord avatar
    diasmannheim
    3 weeks ago

    after making a few minor changes, yeah somehow. and i defined shared variables on railway settings



    here is the code block:


    (server and config file)



    dotenv.config({


    path: path.resolve(__dirname,

    ../.env.${process.env.NODE_ENV}

    ),


    })



    after that i just restart server. and problem solved.



    this:


    https://docs.railway.app/develop/environments#create-an-environment


    so it is necessary to make settings for different env files.



    thats why. 🙂 hope this help

  • default discord avatar
    craigb1988
    3 weeks ago

    @diasmannheim strange one but will give it a go thanks 🙂



    Still doesn't work for me...


    I already have the default production environment setup so I just made all my env variables shared and updated the env path with the code you provided. Exact same error as before.

    Screenshot_2023-11-13_at_16.03.23.png
  • default discord avatar
    diasmannheim
    3 weeks ago

    As a last resort, I uploaded my own env file to github. Honestly, I tried many things and lastly, after deleting the docker settings and restarting the server, I triggered it again with the docker config and somehow it was solved. Frankly, I couldn't understand exactly what solved the problem because the logs were not very helpful.

  • default discord avatar
    craigb1988
    3 weeks ago

    😂 one of those fixes



    Thanks i'll keep looking into this and let you know if i find the source of the fix

  • default discord avatar
    diasmannheim
    3 weeks ago

    sorry :/ but good luck

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.