I had some trouble using environment variables with Next.js and Turbo. The build on Vercel was failing because the environment variables were not populated correctly. I could solve this issue by explicitly telling Turbo that the environment variables exist, as documented here: Using environment variables | Turborepo
I’m not sure if this is the standard way of doing this. My normal workflow (without turbo) was previously:
Create an environment variable within the Vercel dashboard
Run vercel env pull on my machine
Now, with Turbo, it seems that I need to add a third step:
Create an environment variable within the Vercel dashboard
Run vercel env pull on my machine
Add the environment variable to turbo.config
If this is as intended, it would be awesome if the Vercel CLI recognized that I’m using Turbo and therefore added the environment variable to the config automatically.
This would eliminate the third step above, remove any confusion around environment variables, and ultimately improve developer experience and enhance the integration of Vercel and Turbo.
You’re correct about the workflow when using Turborepo with environment variables on Vercel. The three steps you described are indeed the standard approach:
Create environment variables in the Vercel dashboard
Run vercel env pull to get them locally
Add the environment variables to your turbo.json configuration
This is intentional and documented in Vercel’s guidelines for deploying Turborepo projects . The reason for this additional step is that Turborepo needs to know about environment variables for proper caching behavior:
“It’s important to ensure you are managing environment variables correctly. If your project has environment variables, you’ll need to create a list of them in your turbo.json so Turborepo knows to use different caches for different environments.”
Without declaring them in turbo.json, you could accidentally ship your staging environment to production if Turborepo doesn’t know that certain environment variables affect the build output .
You can declare environment variables in your Turborepo configuration in a few ways:
Your suggestion about having the Vercel CLI automatically add environment variables to the Turborepo config is interesting and would improve the developer experience. I will share this feedback internally!