Search code examples
deploymentcontinuous-integrationnext.jsvercel

Passing environment variables to NOW


I am trying to pass firebase environment variables for deployment with now.

I have encoded these variables manually with base64 and added them to now with the following command:
now secrets add firebase_api_key_dev "mybase64string"
The encoded string was placed within speech marks ""

These are in my CLI tool and I can see them all using the list command:
now secrets ls

> 7 secrets found under project-name [499ms]
  name                              created
  firebase_api_key_dev              6d ago
  firebase_auth_domain_dev          6d ago
  ...

In my firebase config, I am using the following code:

const config = {
  apiKey: Buffer.from(process.env.FIREBASE_API_KEY, "base64").toString(),
  authDomain: Buffer.from(process.env.FIREBASE_AUTH_DOMAIN,"base64").toString(),
  ...
}

In my now.json file I have the following code:

{
  "env": {
    "FIREBASE_API_KEY": "@firebase_api_key_dev",
    "FIREBASE_AUTH_DOMAIN": "@firebase_auth_domain_dev",
    ...
  }
}

Everything works fine in my local environment (when I run next) as I also have a .env file with these variables, yet when I deploy my code, I get the following error in my now console:

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type undefined

Does this indicate that my environment variables are not being read? What's the issue here? It looks like they don't exist at all


Solution

  • The solution was to replace my existing now.json with:

    {
      "build":{
        "env": {
          "FIREBASE_API_KEY": "@firebase_api_key",
          "FIREBASE_AUTH_DOMAIN": "@firebase_auth_domain",
          "FIREBASE_DATABASE_URL": "@firebase_database_url",
          "FIREBASE_PROJECT_ID": "@firebase_project_id",
          "FIREBASE_STORAGE_BUCKET": "@firebase_storage_bucket",
          "FIREBASE_MESSAGING_SENDER_ID": "@firebase_messaging_sender_id",
          "FIREBASE_APP_ID": "@firebase_app_id",
          "FIREBASE_API_KEY_DEV": "@firebase_api_key_dev",
          "FIREBASE_AUTH_DOMAIN_DEV": "@firebase_auth_domain_dev",
          "FIREBASE_DATABASE_URL_DEV": "@firebase_database_url_dev",
          "FIREBASE_PROJECT_ID_DEV": "@firebase_project_id_dev",
          "FIREBASE_STORAGE_BUCKET_DEV": "@firebase_storage_bucket_dev",
          "FIREBASE_MESSAGING_SENDER_ID_DEV": "@firebase_messaging_sender_id_dev",
          "FIREBASE_APP_ID_DEV": "@firebase_app_id_dev"
        }
      }  
    }
    

    I was missing the build header.

    I had to contact ZEIT support to help me identify this issue.