dejeszio
dejeszio2w ago

Setting Local Development Environment Variables

I don't know how to set the environment variables for my local development environment, I am unable to get bunx convex dev --local --once to work because some application code requires valid environment variables. Is there a solve for this?
5 Replies
Convex Bot
Convex Bot2w ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
erquhart
erquhart2w ago
You can set the variables on your dev deployment in the dashboard
dejeszio
dejeszioOP2w ago
my dashboard is not getting setup locally, it’s failing i know how to do it in the dashboard but i need to set it before even running convex dev so i want to know if there is a way for me to set the environment variables before i get to the dashboard even support told me that i can use npx convex env - will try So, I have tried but I am getting this error.
x@Jasons-MacBook-Air arb-app % bunx convex dev --local --once
Error: Unable to start push to http://127.0.0.1:3210
Error fetching POST http://127.0.0.1:3210/api/deploy2/start_push 400 Bad Request: InvalidModules: Hit an error while pushing:
Loading the pushed modules encountered the following
error:
Uncaught Failed to analyze _deps/node/PPY4NFXC.js: Unexpected token � in JSON at position 0
x@Jasons-MacBook-Air arb-app % bunx convex dev --local --once
Error: Unable to start push to http://127.0.0.1:3210
Error fetching POST http://127.0.0.1:3210/api/deploy2/start_push 400 Bad Request: InvalidModules: Hit an error while pushing:
Loading the pushed modules encountered the following
error:
Uncaught Failed to analyze _deps/node/PPY4NFXC.js: Unexpected token � in JSON at position 0
I'm not sure if this is my code, or if it's internal code. I have no idea what _deps/node/PPY4NFXC.js is, as I cannot view it. Okay, my fault, my code, I missed a env. So, one thing that I am still not understanding is that when I do convex dev --local --once for the first time, I cannot load envs at all. Not even from a file. I first need to get the local deployment up, and then i can set the environment variables. Am I understanding this correctly? So I have a piece of code that is this:
export function getEnvValOrThrow(name: string): string {
const val = process.env[name];
if (val === undefined) {
throw new ConvexError(`Environment variable '${name}' does not exist`);
}
return val;
}
export function getEnvValOrThrow(name: string): string {
const val = process.env[name];
if (val === undefined) {
throw new ConvexError(`Environment variable '${name}' does not exist`);
}
return val;
}
At the top of files that require an ENV, i have this code:
const polarAccessToken = getEnvValOrThrow("POLAR_ACCESS_TOKEN");
const polarAccessToken = getEnvValOrThrow("POLAR_ACCESS_TOKEN");
When I thus run convex dev --local --once it will never start the deployment locally, so I can never set these environment variables.
dejeszio
dejeszioOP2w ago
So, I am an utter fool. Because the environment for my deployment needs to be configured inside the convex.dev dashboard. I see now that I have my macbook available there as an environment, where i can then add the environment variables. I would suggest we update the documentation, to help guide me that when i do a convex dev --local --once it will render a new machine on convex.dev for the project, so it won't just be production and development. You can then go in there and configure your environment variables. I did not connect the dots with this, and was rather trying to use the .env.local do to this, which is wrong, by design I see. https://docs.convex.dev/cli/local-deployments
Local Deployments for Development | Convex Developer Hub
Instead of syncing code to a Convex dev deployment hosted in the cloud, you can
erquhart
erquhart2w ago
npx convex set env should still have worked for you, did it not? And yeah, .env.local is used just for deployment info by the cli, the code in the deployment doesn't get env vars from that file. Have to be defined either via cli or dashboard.

Did you find this page helpful?