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.

Problem deploying to Railway, missing secret key

default discord avatar
pas1clast year
11

Error:


#11 45.49 unhandledRejection Error: Error: missing secret key. A secret key is needed to secure Payload.



I tried adding PAYLOAD_SECRET as a shared variable but the same error appears.



I also tried changing the dotenv.config to:


dotenv.config({
  path: path.resolve(__dirname, `../.env.${process.env.NODE_ENV}`),
})


I saw that someone had the same issue earlier. Any suggestions on what else I could try?



This is what my dockerfile looks like:


FROM node:18.17-alpine as base

FROM base as builder

WORKDIR /home/node/app
COPY package*.json ./

COPY . .
RUN yarn install
RUN yarn build

FROM base as runtime

ENV NODE_ENV=production
ENV PAYLOAD_CONFIG_PATH=dist/payload.config.js

WORKDIR /home/node/app
COPY package*.json  ./

RUN yarn install --production
COPY --from=builder /home/node/app/dist ./dist
COPY --from=builder /home/node/app/build ./build

EXPOSE 3000

CMD ["node", "dist/server.js"]

`

  • default discord avatar
    nlvogellast year

    What does your

    payload.init

    look like in

    server.ts

    ? That's the only place I have my payload secret and I can deploy fine to Railway.

  • default discord avatar
    pas1clast year

    @nlvogel Did you do any custom config in Railway? It looks like this atm:


    await payload.init({
        express: app,
        onInit: async () => {
          payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`)
        },
        secret: process.env.PAYLOAD_SECRET || '',
      })
  • default discord avatar
    nlvogellast year

    Nope! I just deployed from GitHub. I don't have the

    || ''

    part to my secret, but that shouldn't matter. Do you have

    require('dotenv').config()

    in there? I'm not using a shared variable, either, but I'm not sure that will make a difference.

  • default discord avatar
    pas1clast year

    The secret key was not available when the server is starting. It was happening during the yarn build command as it was configurd like that in some of the payload demos that are using nextjs.



    I separated the build process from the server start process and it fixed the issue.

  • default discord avatar
    _pearcylast year

    @Moris I am having the same issue. How did you separate the build process from the server start?



    I changed the "build" command in package.json and the build process completed but then it crashed.



    This is NOT WORKING

    *I spun up a new version and would recommend that. *


    - leaving it here for REFERENCE and areas to check.


    Here is a good walkthrough tutorial but it's for the admin version with no template.


    https://youtu.be/0XmQE03y5Ek?feature=shared

    For the admin panel + eCommerce template as a monorepo....


    Here are my changes:


    1. in package.json I changed the build script:


    -- The new build script worked for me....


    2. Added a Docker file at root:


    I will leave this here but something is NOT CORRECT in my settings.
    -- images don't show on frontend
    -- issues with GraphQL running correctly.

    adding a Docker file helped but deleting because there is something in my config here or elsewhere that caused issues.
    FROM base as builder
    **

    Check above or other references for Docker file



    3. Make sure you have these variables in Railway:


    PORT=3000
    NODE_ENV=production
    DATABASE_URL=postgresql://postgres:password...etc./railway
    PAYLOAD_SECRET=a1111111
    PAYLOAD_PUBLIC_SERVER_URL=https://dont-forget-the-https://.railway.app
    NEXT_PUBLIC_SERVER_URL=https://dont-forget-the-https://.railway.app

    4. And, of course, these should match what is in your code.


    *** I do have an issue with my images not showing in production which is not a problem when I run it locally.

  • default discord avatar
    jbsselimlast year

    Hello, what do you mean by "-- The new build script worked for me...."

  • default discord avatar
    _pearcylast year

    When I installed Payload CMS locally with eCommerce option this "buld:next" script was in package.json


    "build:next": "cross-env PAYLOAD_CONFIG_PATH=dist/payload/payload.config.js NEXT_BUILD=true node dist/server.js",

    Since I was getting an error (same as others reported when trying to deploy to Railway) this was my attempt to fix it.


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


    This "build" script worked for me:


    `"build:next": "next build",'


    It did install, and I have a working CMS. There are other issues I have which may or may not be related. This was a test and not activley working on the project right now.

  • default discord avatar
    jbsselimlast year

    Thank you for your answer I am actively working on resolving this error right now as I am trying to deploy on Railway & Northflank with Dockerfile



    is this the next cli command for building the project?

  • default discord avatar
    _pearcylast year

    No. You change this script in package.json, then redeploy on Railway.

  • default discord avatar
    jbsselimlast year

    ok ill try thank you

  • default discord avatar
    dan.was.here12 months ago

    Thank you, this also solved it for me to deploy on Railway!


    Simple change the

    build:next

    command

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.