7

I'm not able to solve an error which is mentioned in the title:

Error: $VARIABLE is not listed as a dependency in turbo.json

When I run npm run build I get errors for 2 variables and not for all of them that's what is strange to me...

Error: $NEXT_STRIPE_SK is not listed as a dependency in turbo.json  turbo/no-undeclared-env-vars
Error: $NEXT_PUBLIC_STRIPE_PK is not listed as a dependency in turbo.json  turbo/no-undeclared-env-vars

turbo.json

{
  "$schema": "https://turbo.build/schema.json",
  "pipeline": {
    "build": {
      "dependsOn": [
        "^build"
      ],
      "outputs": [
        "dist/**",
        ".next/**"
      ]
    },
    "order#build": {
      "dependsOn": [
        "^build"
      ],
      "env": [
        "NEXT_PUBLIC_STRIPE_PK",
        "NEXT_STRIPE_SK"
      ],
      "outputs": [
        ".next/**"
      ]
    },
    "lint": {
      "outputs": []
    },
    "dev": {
      "cache": false
    }
  },
  "globalEnv": [
    "NEXT_PUBLIC_SUPABASE_URL",
    "NEXT_PUBLIC_SUPABASE_ANON_KEY",
    "SUPABASE_SERVICE_ROLE",
    "NEXT_PUBLIC_STRIPE_PK",
    "NEXT_STRIPE_SK"
  ],
  "globalDependencies": [
    "$NEXT_PUBLIC_SUPABASE_URL",
    "$NEXT_PUBLIC_SUPABASE_ANON_KEY",
    "$SUPABASE_SERVICE_ROLE",
    "$NEXT_PUBLIC_STRIPE_PK",
    "$NEXT_STRIPE_SK"
  ]
},

This is my .env.local

NEXT_PUBLIC_SUPABASE_URL=
NEXT_PUBLIC_SUPABASE_ANON_KEY=
SUPABASE_SERVICE_ROLE=
NEXT_PUBLIC_STRIPE_PK=
NEXT_STRIPE_SK=
Floky99
  • 562
  • 2
  • 8
  • 17

2 Answers2

2

In NextJS, process.env is actually a dynamic structure that behaves differently from a traditional JavaScript object, despite having a similar interface. Because of this, NextJS can't always know whether a given key will exist on the process.env "object" at runtime. As a result, attempting to access a non-existent key using dot notation, like process.env.someKey, may cause a runtime error.

To avoid this issue, a better practice is to use square bracket syntax to attempt to extract the value from process.env. For example, const value = process.env["someKey"]. This syntax will not cause runtime errors if the key does not exist, and will instead return undefined.

In summary, try the square bracket syntax on environment variables and turbo.json should leave you alone. This is true in NextJS-turbo land, but I haven't checked other frameworks.

Source: I experienced this problem with a turbo project a few minutes ago while building a NextJS package.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 28 '23 at 13:53
1

Based on the configuration

A list of file globs for implicit global hash dependencies. The contents of these files will be included in the global hashing algorithm and affect the hashes of all tasks. This is useful for busting the cache based on .env files (not in Git) or any root level file that impacts workspace tasks (but are not represented in the traditional dependency graph (e.g. a root tsconfig.json, jest.config.js, .eslintrc, etc.)).

you should pass the name of the files to globalDependencies

"globalDependencies": [
    ".env", // contents will impact hashes of all tasks
    ".env.local",
    "tsconfig.json" // contents will impact hashes of all tasks
  ]
Yilmaz
  • 35,338
  • 10
  • 157
  • 202