Skip to main content
In this lesson you will reproduce and fix a common problem that occurs when using Telepresence to intercept a Kubernetes deployment: your locally running process fails because a pod environment variable containing the inventory service URL (API_URL) is not present on your workstation. This guide walks through the failure scenario, explains why it happens, and shows how to export pod environment variables with Telepresence into a .env file and consume them locally with dotenv.

Problem overview

  • The Kubernetes Deployment sets API_URL in the container environment to point at the in-cluster inventory service.
  • When you create a Telepresence intercept and run the application locally, those pod environment variables are not automatically available to the local process.
  • The local process then attempts to call the inventory service with an undefined URL and throws ERR_INVALID_URL.
Example error when API_URL is undefined:
Reproducing snippet that fails when apiURL is undefined:
Note: the snippet above references apiURL directly; unless you set apiURL from process.env.API_URL the variable will be undefined and the request URL will be invalid.

Deployment manifest example (shows API_URL set in the pod)

Why it fails when intercepting

  • The Deployment configures API_URL for the container when running in-cluster.
  • Telepresence intercepts by stopping the real pod traffic and routing traffic to your workstation. It does not automatically inject the pod’s environment variables into your shell.
  • Your local process therefore does not have the same environment (internal DNS names, service ports), causing service calls to fail.

Solution overview

Use Telepresence’s --env-file option to export the pod environment to a file (commonly named .env), then load that file into your local Node.js process with a library such as dotenv. This ensures your local process sees the same values (for example, API_URL) as the in-cluster pod.

Step-by-step reproduction and fix

  1. Make sure there’s no existing intercept for the workload:
  1. Create an intercept and ask Telepresence to write the pod’s environment to a file (by convention .env):
Sample Telepresence output:
  1. Telepresence will create the .env file in your current directory. It contains all the pod environment variables, for example:
  1. Update your local Node.js application to load .env and override any existing environment variables so the local process uses the same values as the pod.
Example Express server that uses dotenv and reads API_URL from process.env:
Once you have the .env file created by Telepresence and you run your local server with the code above, requests that require the inventory service should succeed because API_URL is now defined as it was in-cluster.
Node runtime note: The example uses global fetch — available in Node 18+. If you are on an older Node version, install and import node-fetch (or upgrade Node) before using fetch.

Quick reference: Telepresence commands and files

Summary

  • The Deployment configured API_URL in the pod, but when you intercept and run locally you must provide those same environment variables on your workstation.
  • Telepresence’s --env-file option extracts pod environment variables to a file (commonly .env).
  • Use a library such as dotenv to load that file into your local process so the local behavior matches the in-cluster environment.
Add your .env file to .gitignore and avoid committing it. The .env file may contain internal IPs and service endpoints that should not be checked into source control.

Watch Video