# Croct works in local dev but the turborepo build says Missing environment variable

Asked by Renee Okonkwo on 2025-03-12. Tags: nextjs, monorepo, turborepo, errors.

We recently moved our Next.js app into a turborepo alongside two other packages.
Locally everything runs fine with `next dev`, but CI builds now fail with a
"Missing environment variable" error coming from the Croct SDK.

The confusing part: both `NEXT_PUBLIC_CROCT_APP_ID` and `CROCT_API_KEY` are
definitely set in the pipeline. I can echo them in a step right before the build
and they print correctly. Same variables, same values as before the migration.

Is there something about how turbo runs tasks that hides env vars from the build?
Thanks in advance!

## 1 answer

### Accepted answer from Felix Braun (2025-03-12)

Yes, and it is a Turborepo behavior, not a Croct one. Turbo strips environment
variables that are not declared in `turbo.json`, both to keep task hashes
deterministic and to avoid cache poisoning from undeclared inputs. Your echo
step sees them because it runs outside the turbo task.

Declare them for the build task:

```json
{
  "tasks": {
    "build": {
      "env": ["NEXT_PUBLIC_CROCT_APP_ID", "CROCT_API_KEY"]
    }
  }
}
```

Or put them in `globalEnv` if multiple tasks need them.

The "Missing environment variable" error you are seeing is the SDK's documented
symptom for exactly this. One more subtlety worth knowing: `NEXT_PUBLIC_` vars
are inlined into the bundle at build time, so they must be present during
`next build`, not just at runtime. That is why the failure surfaces in CI rather
than when the container starts.

#### Reply from Renee Okonkwo (2025-03-13)

That was it. Added both to the build task's env array and CI is green again.
The hashing explanation also clarifies why our cache behaved oddly. Much
appreciated, have a great week!
