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"]
`
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.
@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 || '',
})
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.
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.
@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.
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:
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.
Hello, what do you mean by "-- The new build script worked for me...."
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.
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?
No. You change this script in package.json, then redeploy on Railway.
ok ill try thank you
Thank you, this also solved it for me to deploy on Railway!
Simple change the
build:next
command
Star
Discord
online
Get dedicated engineering support directly from the Payload team.