Next.js Discord

Discord Forum

Can't access env variables

Unanswered
Masai Lion posted this in #help-forum
Open in Discord
Avatar
Masai LionOP
I'm trying to set up a vercel project with a postgres database, but I can't seem to access the env variables and actually access the database.

I'm in the root directory, and I run vercel env pull .env.development.local which makes my directory look like this:

webapp
-.env.development.local
-src/
--app/
---api/
----create-project/
-----route.ts


In my route.ts file, this is the code I have:

import { sql } from '@vercel/postgres';

export default async function createProject() {
    console.log({
        POSTGRES_URL: process.env.NEXT_PUBLIC_POSTGRES_URL,
        POSTGRES_URL_NON_POOLING: process.env.NEXT_PUBLIC_POSTGRES_URL_NON_POOLING
    });
}


But the console.log returns undefined for both POSTGRES_URL and POSTGRES_URL_NON_POOLING.

I'm very new to Vercel and NextJS, any ideas on how to fix this? To start local development, I run vercel dev

3 Replies

Avatar
Asian black bear
I'm surprised your console.log even outputs something because that route file you shared is not a correct route and shouldn't even run at all.
Avatar
Masai LionOP
I have a form component that I want to run server code when submitted, what would be a better folder structure for something like that?
Avatar
Asian black bear
It's not about folder structure it's just that route handlers must be exported as named GET, POST, etc. exports and your syntax of a default export is not correct and should error and not run in the first place.