Clarifications regarding environment variables?

Hi I’m building a turborepo app with some file generation apps inside it. Regardless of whether or not I am doing this right (in regards to whether I should be using apps to generate files, usually for other packages), I have some doubts regarding environment variables. One of my apps (gen-gi-helper-data) scrapes data from a google sheet, so I’d normally use a credentials.json file to handle that, however, since Vercel doesn’t support that I’ve resorted to using environment variables to hold the fields of my credentials.json. However I didn’t really make an .env file anywhere and just changed the code to use process.env instead like so:

    credentials: {
      type: process.env.GOOGLEAPIS_TYPE,
      project_id: process.env.GOOGLEAPIS_PROJECT_ID,
      private_key_id: process.env.GOOGLEAPIS_PRIVATE_KEY_ID,
      private_key: process.env.GOOGLEAPIS_PRIVATE_KEY,
      client_email: process.env.GOOGLEAPIS_CLIENT_EMAIL,
      client_id: process.env.GOOGLEAPIS_CLIENT_ID,
      // @ts-ignore
      auth_uri: process.env.GOOGLEAPIS_AUTH_URI,
      token_uri: process.env.GOOGLEAPIS_TOKEN_URI,
      auth_provider_x509_cert_url:
        process.env.GOOGLEAPIS_AUTH_PROVIDER_X509_CERT_URL,
      client_x509_cert_url: process.env.GOOGLEAPIS_CLIENT_X509_CERT_URL,
    }

The @ts-ignore is because the other fields aren’t recognized by googleapis, though just storing this all in a separate object fixes that.

Just changing my code to do this without testing it locally made it work in Vercel (with env variables), however adding a root .env file in my local repo doesn’t work. I’m assuming I’m doing something wrong.

Moreover the docs recommend using .env files per package, trying that doesn’t work either.

When I successfully deployed to Vercel, I got the following warning:

Warning - the following environment variables are set on your Vercel project, but missing from "turbo.json". These variables WILL NOT be available to your application and may cause your build to fail. Learn more at https://turbo.build/repo/docs/platform-environment-variables
[warn] @repo/gi-helper-data#build
[warn]   - GOOGLEAPIS_TYPE 
[warn]   - GOOGLEAPIS_PROJECT_ID 
[warn]   - GOOGLEAPIS_PRIVATE_KEY_ID 
[warn]   - GOOGLEAPIS_PRIVATE_KEY 
[warn]   - GOOGLEAPIS_CLIENT_EMAIL 
[warn]   - GOOGLEAPIS_CLIENT_ID 
[warn]   - GOOGLEAPIS_AUTH_URI 
[warn]   - GOOGLEAPIS_TOKEN_URI 
[warn]   - GOOGLEAPIS_AUTH_PROVIDER_X509_CERT_URL 
[warn]   - GOOGLEAPIS_CLIENT_X509_CERT_URL 
[warn] @repo/unity-richtext-react#build
[warn]   - GOOGLEAPIS_TYPE 
[warn]   - GOOGLEAPIS_PROJECT_ID 
[warn]   - GOOGLEAPIS_PRIVATE_KEY_ID 
[warn]   - GOOGLEAPIS_PRIVATE_KEY 
[warn]   - GOOGLEAPIS_CLIENT_EMAIL 
[warn]   - GOOGLEAPIS_CLIENT_ID 
[warn]   - GOOGLEAPIS_AUTH_URI 
[warn]   - GOOGLEAPIS_TOKEN_URI 
[warn]   - GOOGLEAPIS_AUTH_PROVIDER_X509_CERT_URL 

So I tried adding the following turbo.json to the gen-gi-helper-data app:

// apps/gen-gi-helper-data/turbo.json
{
    "extends": ["//"],
    "tasks": {
        "generate": {
            "env": [
                "GOOGLEAPIS_TYPE",
                "GOOGLEAPIS_PROJECT_ID",
                "GOOGLEAPIS_PRIVATE_KEY_ID",
                "GOOGLEAPIS_PRIVATE_KEY",
                "GOOGLEAPIS_CLIENT_EMAIL",
                "GOOGLEAPIS_CLIENT_ID",
                "GOOGLEAPIS_AUTH_URI",
                "GOOGLEAPIS_TOKEN_URI",
                "GOOGLEAPIS_AUTH_PROVIDER_X509_CERT_URL",
                "GOOGLEAPIS_CLIENT_X509_CERT_URL"
            ]
        }
    }
}

I previously only used pnpm --filter gen-gi-helper-data run generate instead of turbo ..., but adding this and using the appropriate turbo equivalent doesn’t work either.

One more question I have is that .env files are encouraged to be on a per-package/app basis, however Vercel only has a field for what I assume is root level environment variables. So how would I tell Vercel to pass some environment variables to certain packages/apps?

GitHub URL for my monorepo that currently works

Using dotenv-cli method described here does work locally, however I am confused on how this would interact in Vercel.

You’ll need to make sure the environment variables are listed in the turbo.json file.

This guide has more info to guide you through Vercel + Turborepo project config:

Please give that a try and let me know if it works for you!

Hi, sorry for the really late reply, but here’s what I’ve done.
I’ve added turbo tasks for the generations and added “env” to the relevant generate task, however I still get the same warning:

Warning - the following environment variables are set on your Vercel project, but missing from "turbo.json". These variables WILL NOT be available to your application and may cause your build to fail. Learn more at https://turbo.build/repo/docs/platform-environment-variables

My new turbo.json (at root this time, I removed the one in the package):

{
  "$schema": "https://turbo.build/schema.json",
  "ui": "tui",
  "tasks": {
    "build": {
      "dependsOn": ["^build"],
      "inputs": ["$TURBO_DEFAULT$", ".env*"],
      "outputs": [".next/**", "!.next/cache/**", "dist/**"]
    },
    "@repo/gi-helper-data#build": {
      "cache": false
    },
    "generate-files": {
      "dependsOn": ["^generate-files"]
    },
    "gen-gi-data-types#generate-files": {
      "dependsOn": ["@repo/utils#build"],
      "outputs": ["../../packages/gi-data/src/generated/excel-bin-output/**"]
    },
    "gen-gi-data#generate-files": {
      "dependsOn": ["gen-gi-data-types#generate-files", "@repo/gi-data#build"],
      "outputs": ["../../packages/gi-data/src/generated/**", "!../../packages/gi-data/src/generated/excel-bin-output/**"]
    },
    "gen-gi-helper-data#generate-files": {
      "inputs": ["$TURBO_DEFAULT$", ".env*"],
      "env": [
        "GOOGLEAPIS_TYPE",
        "GOOGLEAPIS_PROJECT_ID",
        "GOOGLEAPIS_PRIVATE_KEY_ID",
        "GOOGLEAPIS_PRIVATE_KEY",
        "GOOGLEAPIS_CLIENT_EMAIL",
        "GOOGLEAPIS_CLIENT_ID",
        "GOOGLEAPIS_AUTH_URI",
        "GOOGLEAPIS_TOKEN_URI",
        "GOOGLEAPIS_AUTH_PROVIDER_X509_CERT_URL",
        "GOOGLEAPIS_CLIENT_X509_CERT_URL"
      ],
      "dependsOn": ["gen-gi-data#generate-files", "@repo/gi-data#build"],
      "outputs": ["../../packages/gi-helper-data/src/builds.ts"]
    },
    "lint": {
      "dependsOn": ["^lint"]
    },
    "check-types": {
      "dependsOn": ["^check-types"]
    },
    "dev": {
      "cache": false,
      "persistent": true
    },
    "test": {},
    "test:watch": {
      "cache": false,
      "persistent": true
    }
  }
}

The gen-gi-helper-data#generate-files is the task relevant to having env variables.

Once again since I can only set environment variables in root in Vercel, the warning propogates to the other packages/apps.