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
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
It prints but it does not contain the variables in my .env
Ah
Okay lets try to fix that
can you try to add
import 'dotenv/config'at the top
Then try logging again
it works now, thank you. But may I know why it failed before?
It was working yesterday without dotenv/config
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 😄
Alright, thank you again
Anytime!
Star
Discord
online
Get dedicated engineering support directly from the Payload team.